-
-
Couldn't load subscription status.
- Fork 2.9k
fix: handle multi-type arrays in JSON schema to prevent panic #6495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: handle multi-type arrays in JSON schema to prevent panic #6495
Conversation
Signed-off-by: robert-cronin <robert.owen.cronin@gmail.com>
✅ Deploy Preview for localai ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a panic that occurred when processing JSON schemas with nullable parameters using multi-type arrays (e.g., "type": ["string", "null"]). The code previously assumed type values were always strings and would crash when encountering arrays. The fix adds type checking to handle both single-type strings and multi-type arrays, generating appropriate grammar rules for union types.
Key changes:
- Added type safety when parsing schema type fields to handle both strings and arrays
- Implemented grammar generation for multi-type unions by creating OR rules
- Added comprehensive test coverage for multi-type array scenarios
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| pkg/functions/grammars/json_schema.go | Added type checking logic to handle both single-type strings and multi-type arrays, with grammar generation for union types |
| pkg/functions/grammars/json_schema_test.go | Added test cases covering multi-type array definitions and complex nested schemas with nullable fields |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| if ruleName == "root" { | ||
| schemaType = "root" | ||
| // Handle primitive types, including multi-type arrays like ["string", "null"] | ||
| if len(schemaTypes) > 1 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a small style nit: would make sense to contract this and make it an else if ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good, thanks - good catch!
Description
This PR fixes #5572
When you send a function definition to LocalAI with nullable parameters (using
"type": ["string", "null"]like OpenAI does in strict mode), the server would immediately crash. This happened because the code assumed type was always a single string and tried to force-convert it, which panics when it's actually an array.Notes for Reviewers
Signed commits