{"record":{"id":"a19693f49e05ec62","repo":"Tencent/WeKnora","slug":"failed-to-marshal-schema-v","errorCode":null,"errorMessage":"failed to marshal schema: %v","messagePattern":"failed to marshal schema: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/utils/json.go","lineNumber":31,"sourceCode":"\tif err != nil {\n\t\treturn \"\"\n\t}\n\treturn string(json)\n}\n\n// GenerateSchema generates JSON schema for type T and returns it as a map\n// This is optimized to avoid unnecessary serialization/deserialization\nfunc GenerateSchema[T any]() json.RawMessage {\n\tschema, err := jsonschema.For[T](nil)\n\tif err != nil {\n\t\tpanic(fmt.Sprintf(\"failed to generate schema: %v\", err))\n\t}\n\n\t// Convert schema to map directly through JSON marshaling\n\t// This is necessary because the schema object doesn't expose its internal structure\n\tschemaBytes, err := json.Marshal(schema)\n\tif err != nil {\n\t\tpanic(fmt.Sprintf(\"failed to marshal schema: %v\", err))\n\t}\n\n\treturn schemaBytes\n}\n","sourceCodeStart":13,"sourceCodeEnd":36,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/utils/json.go#L13-L36","documentation":"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.","triggerScenarios":"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.","commonSituations":"Passing custom schema options/extensions whose types cannot marshal; a library version regression; embedding types with faulty MarshalJSON into tool input structs.","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"],"exampleFix":"// before\nschema, err := jsonschema.For[T](opts) // opts contains custom unmarshalable extension\n// after\nschema, err := jsonschema.For[T](nil) // drop unsupported options","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"func marshalSchemaSafe(schema *jsonschema.Schema) (b []byte, err error) {\n    defer func() { if r := recover(); r != nil { err = fmt.Errorf(\"schema marshal panic: %v\", r) } }()\n    return json.Marshal(schema)\n}","preventionTips":["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"],"tags":["go","panic","json","marshaling","json-schema","tools"],"backgroundTag":"schema-generation-failed","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}