Tencent/WeKnora · error
failed to marshal schema: %v
Error message
failed to marshal schema: %v
What it means
After generating the schema, GenerateSchema[T] marshals it to JSON bytes. json.Marshal on a generated *jsonschema.Schema should rarely fail, but if it does (unsupported values introduced via custom keywords/extensions or a marshaling bug), the function panics.
Source
Thrown at internal/utils/json.go:31
if err != nil {
return ""
}
return string(json)
}
// GenerateSchema generates JSON schema for type T and returns it as a map
// This is optimized to avoid unnecessary serialization/deserialization
func GenerateSchema[T any]() json.RawMessage {
schema, err := jsonschema.For[T](nil)
if err != nil {
panic(fmt.Sprintf("failed to generate schema: %v", err))
}
// Convert schema to map directly through JSON marshaling
// This is necessary because the schema object doesn't expose its internal structure
schemaBytes, err := json.Marshal(schema)
if err != nil {
panic(fmt.Sprintf("failed to marshal schema: %v", err))
}
return schemaBytes
}
View on GitHub (pinned to 988cbb0330)
Solutions
- Inspect the schema options/extensions passed to jsonschema.For and remove or fix unmarshalable custom values
- Update the invopop/jsonschema dependency to a version fixing the marshal error
- Prefer the returned json.RawMessage path without custom extensions, or build the schema map manually
- Add a recover in tool initialization to identify which tool's type triggers it
Example fix
// before schema, err := jsonschema.For[T](opts) // opts contains custom unmarshalable extension // after schema, err := jsonschema.For[T](nil) // drop unsupported options
Defensive patterns
Strategy: try-catch
Try / catch
func marshalSchemaSafe(schema *jsonschema.Schema) (b []byte, err error) {
defer func() { if r := recover(); r != nil { err = fmt.Errorf("schema marshal panic: %v", r) } }()
return json.Marshal(schema)
} Prevention
- Avoid passing custom extensions/options with faulty MarshalJSON implementations to jsonschema.For
- Keep the invopop/jsonschema dependency up to date
- Add a startup smoke test that marshals every tool's schema
When it happens
Trigger: json.Marshal(schema) returning an error — practically only when the schema contains values with custom MarshalJSON implementations that error, or corrupt/unsupported structures injected through schema options/extensions passed to jsonschema.For.
Common situations: Passing custom schema options/extensions whose types cannot marshal; a library version regression; embedding types with faulty MarshalJSON into tool input structs.
Related errors
- failed to generate schema: %v
- failed to marshal Exa request: %w
- failed to set FAQ metadata: %w
- marshal credentials: %w
- parse rss credentials: %w
AI-assisted analysis of Tencent/WeKnora@988cbb0330 (2026-09-02).
Data as JSON: /api/errors/a19693f49e05ec62.
Report an issue: GitHub.