github/copilot-sdk · error
failed to unmarshal schema for type
Error message
failed to unmarshal schema for type %v: %v
What it means
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.
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
Example fix
// before
var schemaMap map[string]any
json.Unmarshal(schemaBytes, &schemaMap)
// after — ensure top-level is an object
var probe any
json.Unmarshal(schemaBytes, &probe)
if _, ok := probe.(map[string]any); !ok { /* ForType returned non-object schema */ } Defensive patterns
Strategy: try-catch
Validate before calling
var probe any
if err := json.Unmarshal(schemaBytes, &probe); err != nil {
return fmt.Errorf("schema JSON invalid: %w", err)
}
if _, ok := probe.(map[string]any); !ok {
return fmt.Errorf("schema is not a JSON object")
} Type guard
func isJSONObject(b []byte) bool {
var m map[string]any
return json.Unmarshal(b, &m) == nil && m != nil
} Try / catch
defer func() {
if r := recover(); r != nil {
log.Printf("schema conversion failed: %v", r)
}
}() Prevention
- 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
When it happens
Trigger: 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.
Common situations: Build/toolchain anomalies, monkey-patched or vendored encoding/json, or downstream code replacing this function with one that marshals differently.
Understand the failure class
Background: "failed to unmarshal" / json.Unmarshal errors: why parsing a response into a Go struct fails and how to fix it — this error's family across 23 libraries.
Related errors
- failed to unmarshal models response
- failed to unmarshal get events response
- failed to decode session detach response
- failed to marshal schema for type
- failed to unmarshal response
AI-assisted analysis of github/copilot-sdk@cd8cf15dc3 (2026-09-09).
Data as JSON: /api/errors/76a7e394b6ffc7f3.
Report an issue: GitHub.
Appendix: source
Thrown at go/definetool.go:228
if t.Kind() == reflect.Pointer {
t = t.Elem()
}
// Use google/jsonschema-go to generate the schema
schema, err := jsonschema.ForType(t, nil)
if err != nil {
panic(fmt.Sprintf("failed to generate schema for type %v: %v", t, err))
}
// Convert schema to map[string]any
schemaBytes, err := json.Marshal(schema)
if err != nil {
panic(fmt.Sprintf("failed to marshal schema for type %v: %v", t, err))
}
var schemaMap map[string]any
if err := json.Unmarshal(schemaBytes, &schemaMap); err != nil {
panic(fmt.Sprintf("failed to unmarshal schema for type %v: %v", t, err))
}
return schemaMap
}
View on GitHub (pinned to cd8cf15dc3)