{"record":{"id":"a08f20a2d7c23197","repo":"github/copilot-sdk","slug":"failed-to-generate-schema-for-type-v-v","errorCode":null,"errorMessage":"failed to generate schema for type %v: %v","messagePattern":"failed to generate schema for type (.+?): (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/definetool.go","lineNumber":217,"sourceCode":"\treturn tr, true\n}\n\n// generateSchemaForType generates a JSON schema map from a Go type using reflection.\n// Panics if schema generation fails, as this indicates a programming error.\nfunc generateSchemaForType(t reflect.Type) map[string]any {\n\tif t == nil {\n\t\treturn nil\n\t}\n\n\t// Handle pointer types\n\tif t.Kind() == reflect.Pointer {\n\t\tt = t.Elem()\n\t}\n\n\t// Use google/jsonschema-go to generate the schema\n\tschema, err := jsonschema.ForType(t, nil)\n\tif err != nil {\n\t\tpanic(fmt.Sprintf(\"failed to generate schema for type %v: %v\", t, err))\n\t}\n\n\t// Convert schema to map[string]any\n\tschemaBytes, err := json.Marshal(schema)\n\tif err != nil {\n\t\tpanic(fmt.Sprintf(\"failed to marshal schema for type %v: %v\", t, err))\n\t}\n\n\tvar schemaMap map[string]any\n\tif err := json.Unmarshal(schemaBytes, &schemaMap); err != nil {\n\t\tpanic(fmt.Sprintf(\"failed to unmarshal schema for type %v: %v\", t, err))\n\t}\n\n\treturn schemaMap\n}\n","sourceCodeStart":199,"sourceCodeEnd":233,"githubUrl":"https://github.com/github/copilot-sdk/blob/cd8cf15dc3f9e762615790aaed0a771a0f392755/go/definetool.go#L199-L233","documentation":"generateSchemaForType uses google/jsonschema-go (jsonschema.ForType) to derive a JSON schema from a Go type passed to DefineTool. If ForType cannot build a schema for the (dereferenced) reflect.Type, the function panics with the type and underlying error. DefineTool requires every parameter type to be representable as a JSON schema.","triggerScenarios":"Calling DefineTool with a parameter struct/type that jsonschema.ForType cannot handle — e.g. types containing unsupported fields (funcs, chans, unexported-only fields, unsupported map key types) or a nil/invalid reflect type reached after pointer dereferencing (t.Elem()).","commonSituations":"Defining a tool whose input struct includes channels, function fields, complex numbers, or custom types without JSON-schema support; upgrading the jsonschema-go library changes what is supported; accidentally passing a non-struct such as a func type.","solutions":["Simplify the tool input type to JSON-representable fields (strings, numbers, bools, slices, maps, nested structs)","Remove unsupported field types (func, chan, unsafe pointers) or wrap them behind a supported representation","Check the wrapped error for the exact field/type jsonschema.ForType rejected","Dereference expectations: pass the concrete value type rather than exotic pointer/channel types"],"exampleFix":"// before\ntype In struct { Callback func(string) `json:\"cb\"` }\nDefineTool(\"t\", \"d\", In{})\n// after\ntype In struct { CallbackName string `json:\"callbackName\"` }\nDefineTool(\"t\", \"d\", In{})","handlingStrategy":"validation","validationCode":"func jsonSchemaSafe(t reflect.Type) error {\n\tfor t.Kind() == reflect.Pointer { t = t.Elem() }\n\t_, err := jsonschema.ForType(t, nil)\n\treturn err\n}","typeGuard":"func isJSONSchemaSafeKind(k reflect.Kind) bool {\n\tswitch k {\n\tcase reflect.Struct, reflect.Map, reflect.Slice, reflect.Array,\n\t\treflect.String, reflect.Bool,\n\t\treflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,\n\t\treflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,\n\t\treflect.Float32, reflect.Float64, reflect.Interface:\n\t\treturn true\n\t}\n\treturn false\n}","tryCatchPattern":null,"preventionTips":["Keep tool input structs limited to JSON-serializable field types","Ban func/chan/complex fields in tool parameter types via a unit test that calls generateSchemaForType on every tool input","Test schema generation for all DefineTool types in CI","Review jsonschema-go release notes before upgrading"],"tags":["go","jsonschema","reflection","panic"],"backgroundTag":"schema-validation-failed","analyzedSha":"cd8cf15dc3f9e762615790aaed0a771a0f392755","analyzedAt":"2026-09-09T18:32:31.973Z","contentChangedAt":"2026-09-09T18:32:31.973Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}