github/copilot-sdk · error

failed to marshal schema for type

Error message

failed to marshal schema for type %v: %v

What it means

After jsonschema.ForType succeeds, generateSchemaForType marshals the resulting schema to JSON with encoding/json. A marshal failure panics with the type and error. Because the schema value comes from the jsonschema library, this is rare and indicates the schema object itself is not JSON-serializable.

Solutions

  1. Check the wrapped marshal error to identify the offending schema member
  2. Upgrade or pin a compatible version of google/jsonschema-go
  3. Simplify the tool input type so the generated schema stays plain
  4. Marshal the schema yourself in a test to reproduce and isolate the failing field

Example fix

// before
schema, err := jsonschema.ForType(reflect.TypeOf(In{}), nil)
// after — test the schema round-trips
var check map[string]any
b, _ := json.Marshal(schema)
if err := json.Unmarshal(b, &check); err != nil { /* simplify type */ }
Defensive patterns

Strategy: validation

Validate before calling

schema, err := jsonschema.ForType(t, nil)
if err == nil {
	if _, err := json.Marshal(schema); err != nil {
		return fmt.Errorf("schema for %v not serializable: %w", t, err)
	}
}

Prevention

When it happens

Trigger: json.Marshal failing on the schema returned by jsonschema.ForType — e.g. a schema containing values that encoding/json cannot serialize (channels, funcs, NaN/Inf floats) possibly due to a custom schema field or library version change.

Common situations: Pinned/incompatible versions of google/jsonschema-go producing schema objects with unsupported member types; custom types with MarshalJSON methods that return invalid values; tool input structs with unusual field types.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


AI-assisted analysis of github/copilot-sdk@cd8cf15dc3 (2026-09-09). Data as JSON: /api/errors/a9a3adc1e807dfdf. Report an issue: GitHub.

Appendix: source

Thrown at go/definetool.go:223

	if t == nil {
		return nil
	}

	// Handle pointer types
	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)