{"record":{"id":"76a7e394b6ffc7f3","repo":"github/copilot-sdk","slug":"failed-to-unmarshal-schema-for-type-v-v","errorCode":null,"errorMessage":"failed to unmarshal schema for type %v: %v","messagePattern":"failed to unmarshal schema for type (.+?): (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/definetool.go","lineNumber":228,"sourceCode":"\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":210,"sourceCodeEnd":233,"githubUrl":"https://github.com/github/copilot-sdk/blob/cd8cf15dc3f9e762615790aaed0a771a0f392755/go/definetool.go#L210-L233","documentation":"generateSchemaForType round-trips the marshaled schema through json.Unmarshal into map[string]any before returning it. If the JSON just produced by json.Marshal cannot be unmarshaled into a map, it panics. In practice this is an internal invariant: freshly marshaled schema JSON should always decode, so this points at corrupted marshaling or memory issues.","triggerScenarios":"json.Unmarshal returning an error on schemaBytes that were marshaled in the immediately preceding step — practically only when marshaling produced invalid/large/truncated JSON or an environment-level fault; also seen in altered builds where schemaBytes was substituted.","commonSituations":"Build/toolchain anomalies, monkey-patched or vendored encoding/json, or downstream code replacing this function with one that marshals differently.","solutions":["Verify the schema marshals to a JSON object (not an array/scalar) — top-level must decode into map[string]any","Rebuild without vendored/patched encoding/json","Inspect schemaBytes content if you modified the function","Report upstream if reproducible with an unmodified library"],"exampleFix":"// before\nvar schemaMap map[string]any\njson.Unmarshal(schemaBytes, &schemaMap)\n// after — ensure top-level is an object\nvar probe any\njson.Unmarshal(schemaBytes, &probe)\nif _, ok := probe.(map[string]any); !ok { /* ForType returned non-object schema */ }","handlingStrategy":"try-catch","validationCode":"var probe any\nif err := json.Unmarshal(schemaBytes, &probe); err != nil {\n\treturn fmt.Errorf(\"schema JSON invalid: %w\", err)\n}\nif _, ok := probe.(map[string]any); !ok {\n\treturn fmt.Errorf(\"schema is not a JSON object\")\n}","typeGuard":"func isJSONObject(b []byte) bool {\n\tvar m map[string]any\n\treturn json.Unmarshal(b, &m) == nil && m != nil\n}","tryCatchPattern":"defer func() {\n\tif r := recover(); r != nil {\n\t\tlog.Printf(\"schema conversion failed: %v\", r)\n\t}\n}()","preventionTips":["Do not modify the marshal/unmarshal round-trip code","Keep encoding/json unpatched and standard","Add a regression test asserting the round-trip succeeds for all tool types"],"tags":["go","json","unmarshal","panic"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"cd8cf15dc3f9e762615790aaed0a771a0f392755","analyzedAt":"2026-09-09T18:32:31.973Z","contentChangedAt":"2026-09-09T18:32:31.973Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}