Return errors from schema generation instead of panicking

jsonSchemaFor panicked on unsupported types, which meant
FunctionTool, NewOutputType, and RunTyped would crash the
process during setup rather than returning a normal error.

Signed-off-by: Bryan Frimin <bryan@getprobo.com>
This commit is contained in:
Bryan Frimin
2026-03-13 18:56:56 +01:00
parent def8f417ca
commit f3239a1a7b
12 changed files with 505 additions and 406 deletions

View File

@@ -22,22 +22,30 @@ import (
"github.com/google/jsonschema-go/jsonschema"
)
func jsonSchemaFor[T any]() json.RawMessage {
func jsonSchemaFor[T any]() (json.RawMessage, error) {
t := reflect.TypeFor[T]()
schema, err := jsonschema.ForType(t, nil)
if err != nil {
panic(fmt.Sprintf("cannot generate schema for %s: %v", t, err))
return nil, fmt.Errorf("cannot generate schema for %s: %w", t, err)
}
stripNullTypes(schema)
data, err := json.Marshal(schema)
if err != nil {
panic(fmt.Sprintf("cannot marshal schema for %s: %v", t, err))
return nil, fmt.Errorf("cannot marshal schema for %s: %w", t, err)
}
return json.RawMessage(data)
return json.RawMessage(data), nil
}
func mustJSONSchemaFor[T any]() json.RawMessage {
schema, err := jsonSchemaFor[T]()
if err != nil {
panic(err)
}
return schema
}
// stripNullTypes removes "null" from union types produced by pointer fields