| 1 | package anthropicscip-go gomod github.com/anthropics/anthropic-sdk-go . `github.com/anthropics/anthropic-sdk-go`/ |
| 2 | |
| 3 | import ( |
| 4 | "fmtscip-go gomod github.com/golang/go/src go1.23.0 fmt/" |
| 5 | "mapsscip-go gomod github.com/golang/go/src go1.23.0 maps/" |
| 6 | "slicesscip-go gomod github.com/golang/go/src go1.23.0 slices/" |
| 7 | "sortscip-go gomod github.com/golang/go/src go1.23.0 sort/" |
| 8 | "stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/" |
| 9 | ) |
| 10 | |
| 11 | // BetaJSONSchemaOutputFormat creates a BetaJSONOutputFormatParam from a JSON schema map. |
| 12 | // It transforms the schema to ensure compatibility with Anthropic's JSON schema requirements. |
| 13 | // |
| 14 | // Example: |
| 15 | // |
| 16 | // schema := map[string]any{ |
| 17 | // "type": "object", |
| 18 | // "properties": map[string]any{ |
| 19 | // "name": map[string]any{"type": "string"}, |
| 20 | // "age": map[string]any{"type": "integer", "minimum": 0}, |
| 21 | // }, |
| 22 | // "required": []string{"name"}, |
| 23 | // } |
| 24 | // outputFormat := BetaJSONSchemaOutputFormat(schema) |
| 25 | // |
| 26 | // msg, _ := client.Beta.Messages.New(ctx, anthropic.BetaMessageNewParams{ |
| 27 | // Model: anthropic.Model("claude-sonnet-4-5"), |
| 28 | // Messages: anthropic.F([]anthropic.BetaMessageParam{...}), |
| 29 | // MaxTokens: 1024, |
| 30 | // OutputFormat: outputFormat, |
| 31 | // }) |
| 32 | func BetaJSONSchemaOutputFormatscip-go gomod github.com/anthropics/anthropic-sdk-go . `github.com/anthropics/anthropic-sdk-go`/BetaJSONSchemaOutputFormat().(jsonSchema map[string]any) BetaJSONOutputFormatParamscip-go gomod github.com/anthropics/anthropic-sdk-go . `github.com/anthropics/anthropic-sdk-go`/BetaJSONOutputFormatParam# { |
| 33 | return BetaJSONOutputFormatParamscip-go gomod github.com/anthropics/anthropic-sdk-go . `github.com/anthropics/anthropic-sdk-go`/BetaJSONOutputFormatParam#{Schemascip-go gomod github.com/anthropics/anthropic-sdk-go . `github.com/anthropics/anthropic-sdk-go`/BetaJSONOutputFormatParam#Schema.: transformSchemascip-go gomod github.com/anthropics/anthropic-sdk-go . `github.com/anthropics/anthropic-sdk-go`/transformSchema().(jsonSchema)} |
| 34 | } |
| 35 | |
| 36 | // BetaToolInputSchema creates a BetaToolInputSchemaParam from a JSON schema map. |
| 37 | // It transforms the schema to ensure compatibility with Anthropic's tool calling requirements. |
| 38 | func BetaToolInputSchemascip-go gomod github.com/anthropics/anthropic-sdk-go . `github.com/anthropics/anthropic-sdk-go`/BetaToolInputSchema().(jsonSchema map[string]any) BetaToolInputSchemaParamscip-go gomod github.com/anthropics/anthropic-sdk-go . `github.com/anthropics/anthropic-sdk-go`/BetaToolInputSchemaParam# { |
| 39 | return BetaToolInputSchemaParamscip-go gomod github.com/anthropics/anthropic-sdk-go . `github.com/anthropics/anthropic-sdk-go`/BetaToolInputSchemaParam#{ExtraFieldsscip-go gomod github.com/anthropics/anthropic-sdk-go . `github.com/anthropics/anthropic-sdk-go`/BetaToolInputSchemaParam#ExtraFields.: transformSchemascip-go gomod github.com/anthropics/anthropic-sdk-go . `github.com/anthropics/anthropic-sdk-go`/transformSchema().(jsonSchema)} |
| 40 | } |
| 41 | |
| 42 | var supportedStringFormatsscip-go gomod github.com/anthropics/anthropic-sdk-go . `github.com/anthropics/anthropic-sdk-go`/supportedStringFormats. = []string{ |
| 43 | "date-time", |
| 44 | "time", |
| 45 | "date", |
| 46 | "duration", |
| 47 | "email", |
| 48 | "hostname", |
| 49 | "uri", |
| 50 | "ipv4", |
| 51 | "ipv6", |
| 52 | "uuid", |
| 53 | } |
| 54 | var supportedSchemaKeysscip-go gomod github.com/anthropics/anthropic-sdk-go . `github.com/anthropics/anthropic-sdk-go`/supportedSchemaKeys. = []string{ |
| 55 | // Top-level schema keys |
| 56 | "$ref", |
| 57 | "$defs", |
| 58 | "type", |
| 59 | "anyOf", |
| 60 | "oneOf", |
| 61 | "description", |
| 62 | "title", |
| 63 | |
| 64 | // Object-specific keys |
| 65 | "properties", |
| 66 | "additionalProperties", |
| 67 | "required", |
| 68 | |
| 69 | // Array-specific keys |
| 70 | "items", |
| 71 | "minItems", |
| 72 | |
| 73 | // String-specific keys |
| 74 | "format", |
| 75 | } |
| 76 | |
| 77 | // TransformSchema transforms a JSON schema to ensure it conforms to the Anthropic API's expectations. |
| 78 | // It returns nil if the transformed schema is empty. |
| 79 | // |
| 80 | // The transformation process: |
| 81 | // - Preserves $ref references |
| 82 | // - Transforms $defs recursively |
| 83 | // - Handles anyOf/oneOf by converting oneOf to anyOf |
| 84 | // - Ensures objects have additionalProperties: false |
| 85 | // - Filters string formats to only supported ones |
| 86 | // - Limits array minItems to 0 or 1 |
| 87 | // - Appends unsupported properties to the description |
| 88 | // |
| 89 | // Example: |
| 90 | // |
| 91 | // schema := map[string]any{ |
| 92 | // "type": "integer", |
| 93 | // "minimum": 1, |
| 94 | // "maximum": 10, |
| 95 | // "description": "A number", |
| 96 | // } |
| 97 | // transformed := TransformSchema(schema) |
| 98 | // // Result: {"type": "integer", "description": "A number\n\n{minimum: 1, maximum: 10}"} |
| 99 | func transformSchemascip-go gomod github.com/anthropics/anthropic-sdk-go . `github.com/anthropics/anthropic-sdk-go`/transformSchema().(jsonSchema map[string]any) map[string]any { |
| 100 | if jsonSchema == nil { |
| 101 | return nil |
| 102 | } |
| 103 | |
| 104 | strictSchema := make(map[string]any) |
| 105 | |
| 106 | // Create a copy to avoid modifying the original |
| 107 | schemaCopy := make(map[string]any) |
| 108 | mapsscip-go gomod github.com/golang/go/src go1.23.0 maps/.Copyscip-go gomod github.com/golang/go/src go1.23.0 maps/Copy().(schemaCopy, jsonSchema) |
| 109 | |
| 110 | // $ref is not supported alongside other properties |
| 111 | if ref, ok := schemaCopy["$ref"]; ok { |
| 112 | strictSchema["$ref"] = ref |
| 113 | return strictSchema |
| 114 | } |
| 115 | |
| 116 | for _, key := range supportedSchemaKeysscip-go gomod github.com/anthropics/anthropic-sdk-go . `github.com/anthropics/anthropic-sdk-go`/supportedSchemaKeys. { |
| 117 | value, exists := schemaCopy[key] |
| 118 | if exists { |
| 119 | delete(schemaCopy, key) |
| 120 | strictSchema[key] = value |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if defs, ok := strictSchema["$defs"]; ok { |
| 125 | if defsMap, ok := defs.(map[string]any); ok { |
| 126 | strictDefs := make(map[string]any) |
| 127 | strictSchema["$defs"] = strictDefs |
| 128 | |
| 129 | for name, schema := range defsMap { |
| 130 | if schemaMap, ok := schema.(map[string]any); ok { |
| 131 | strictDefs[name] = transformSchemascip-go gomod github.com/anthropics/anthropic-sdk-go . `github.com/anthropics/anthropic-sdk-go`/transformSchema().(schemaMap) |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | typeValue, _ := strictSchema["type"] |
| 138 | anyOf, _ := strictSchema["anyOf"] |
| 139 | oneOf, _ := strictSchema["oneOf"] |
| 140 | |
| 141 | if anyOfSlice, ok := anyOf.([]any); ok { |
| 142 | transformedVariants := make([]any, 0, len(anyOfSlice)) |
| 143 | for _, variant := range anyOfSlice { |
| 144 | variantMap, ok := variant.(map[string]any) |
| 145 | if !ok { |
| 146 | continue |
| 147 | } |
| 148 | if transformed := transformSchemascip-go gomod github.com/anthropics/anthropic-sdk-go . `github.com/anthropics/anthropic-sdk-go`/transformSchema().(variantMap); transformed != nil { |
| 149 | transformedVariants = append(transformedVariants, transformed) |
| 150 | } |
| 151 | } |
| 152 | strictSchema["anyOf"] = transformedVariants |
| 153 | } else if oneOfSlice, ok := oneOf.([]any); ok { |
| 154 | transformedVariants := make([]any, 0, len(oneOfSlice)) |
| 155 | for _, variant := range oneOfSlice { |
| 156 | if variantMap, ok := variant.(map[string]any); ok { |
| 157 | if transformed := transformSchemascip-go gomod github.com/anthropics/anthropic-sdk-go . `github.com/anthropics/anthropic-sdk-go`/transformSchema().(variantMap); transformed != nil { |
| 158 | transformedVariants = append(transformedVariants, transformed) |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | delete(strictSchema, "oneOf") |
| 163 | strictSchema["anyOf"] = transformedVariants |
| 164 | } else { |
| 165 | if typeValue == nil { |
| 166 | // schema is completely invalid, we have to bail |
| 167 | return nil |
| 168 | } |
| 169 | |
| 170 | strictSchema["type"] = typeValue |
| 171 | } |
| 172 | |
| 173 | typeStr, _ := typeValue.(string) |
| 174 | switch typeStr { |
| 175 | case "object": |
| 176 | if properties, ok := strictSchema["properties"]; ok { |
| 177 | if propsMap, ok := properties.(map[string]any); ok { |
| 178 | transformedProps := make(map[string]any) |
| 179 | for key, propSchema := range propsMap { |
| 180 | if propSchemaMap, ok := propSchema.(map[string]any); ok { |
| 181 | transformedProps[key] = transformSchemascip-go gomod github.com/anthropics/anthropic-sdk-go . `github.com/anthropics/anthropic-sdk-go`/transformSchema().(propSchemaMap) |
| 182 | } |
| 183 | } |
| 184 | strictSchema["properties"] = transformedProps |
| 185 | } |
| 186 | } else { |
| 187 | strictSchema["properties"] = make(map[string]any) |
| 188 | } |
| 189 | |
| 190 | strictSchema["additionalProperties"] = false |
| 191 | |
| 192 | case "string": |
| 193 | if format, ok := strictSchema["format"]; ok { |
| 194 | if formatStr, ok := format.(string); ok { |
| 195 | if !slicesscip-go gomod github.com/golang/go/src go1.23.0 slices/.Containsscip-go gomod github.com/golang/go/src go1.23.0 slices/Contains().(supportedStringFormatsscip-go gomod github.com/anthropics/anthropic-sdk-go . `github.com/anthropics/anthropic-sdk-go`/supportedStringFormats., formatStr) { |
| 196 | schemaCopy["format"] = format |
| 197 | delete(strictSchema, "format") |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | case "array": |
| 203 | if items, ok := strictSchema["items"]; ok { |
| 204 | if itemsMap, ok := items.(map[string]any); ok { |
| 205 | strictSchema["items"] = transformSchemascip-go gomod github.com/anthropics/anthropic-sdk-go . `github.com/anthropics/anthropic-sdk-go`/transformSchema().(itemsMap) |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | if minItems, ok := strictSchema["minItems"]; ok { |
| 210 | if minItems != 0 && minItems != 1 { |
| 211 | schemaCopy["minItems"] = minItems |
| 212 | delete(strictSchema, "minItems") |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | case "boolean", "integer", "number", "null": |
| 217 | // These types are supported as-is |
| 218 | } |
| 219 | |
| 220 | if len(schemaCopy) > 0 { |
| 221 | description := strictSchema["description"] |
| 222 | descStr, _ := description.(string) |
| 223 | |
| 224 | // Sort keys for deterministic output |
| 225 | keys := make([]string, 0, len(schemaCopy)) |
| 226 | for key := range schemaCopy { |
| 227 | keys = append(keys, key) |
| 228 | } |
| 229 | sortscip-go gomod github.com/golang/go/src go1.23.0 sort/.Stringsscip-go gomod github.com/golang/go/src go1.23.0 sort/Strings().(keys) |
| 230 | |
| 231 | extraProps := make([]string, 0, len(keys)) |
| 232 | for _, key := range keys { |
| 233 | extraProps = append(extraProps, fmtscip-go gomod github.com/golang/go/src go1.23.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.23.0 fmt/Sprintf().("%s: %v", key, schemaCopy[key])) |
| 234 | } |
| 235 | |
| 236 | if descStr != "" { |
| 237 | strictSchema["description"] = descStr + "\n\n{" + stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.23.0 strings/Join().(extraProps, ", ") + "}" |
| 238 | } else { |
| 239 | strictSchema["description"] = "{" + stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.23.0 strings/Join().(extraProps, ", ") + "}" |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | return strictSchema |
| 244 | } |