Skip to content

Conversation

@robert-cronin
Copy link
Contributor

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

  • Yes, I signed my commits.

Signed-off-by: robert-cronin <robert.owen.cronin@gmail.com>
@netlify
Copy link

netlify bot commented Oct 17, 2025

Deploy Preview for localai ready!

Name Link
🔨 Latest commit 8d5e7db
🔍 Latest deploy log https://app.netlify.com/projects/localai/deploys/68f19e8c7d40cf00089ee6e4
😎 Deploy Preview https://deploy-preview-6495--localai.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link

Copilot AI left a 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 {
Copy link
Owner

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 ...

Copy link
Owner

@mudler mudler left a 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!

@mudler mudler merged commit 639ecb5 into mudler:master Oct 17, 2025
27 of 28 checks passed
@mudler mudler added the bug Something isn't working label Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

panic: interface conversion: interface {} is []interface {}, not string with OpenAI compatible function defintion

2 participants