Skip to content

fix: preserve array items property in union schemas for MCP compatibility #166

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

combiz
Copy link

@combiz combiz commented Jun 11, 2025

Describe your changes

When converting OpenAPI schemas with Union types containing arrays (e.g., Union[str, List[str]]), the array variant was missing the required 'items' property. This caused validation errors with OpenAI and other LLMs.

Error example:

litellm.BadRequestError: OpenAIException - Error code: 400 - {
  'error': {
    'message': "Invalid schema for function 'stemmed_count_keywords_count_keywords_post_409a8e':
                In context=('properties', 'text'), array schema missing items.",
    'type': 'invalid_request_error',
    'param': 'tools[0].function.parameters',
    'code': 'invalid_function_parameters'
  }
}

The fix introduces simplify_union_schema() which:

  • Detects Union types (anyOf/oneOf in OpenAPI)
  • Selects the first non-null type from the union
  • Preserves ALL properties including 'items' for arrays
  • Maintains title, description, and default from parent schema

This ensures compatibility with LLMs that require strict JSON schema validation for function parameters.

Fixes issues with common FastAPI patterns like:

  • Union[str, List[str]] - single or multiple values
  • Optional[List[T]] - optional arrays
  • Union[str, List[Model]] - mixed type unions

Issue ticket number and link (if applicable)

#165

Checklist before requesting a review

  • Added relevant tests
  • Run ruff & mypy
  • All tests pass

…lity

When converting OpenAPI schemas with Union types containing arrays (e.g.,
Union[str, List[str]]), the array variant was missing the required 'items'
property. This caused validation errors with OpenAI and other LLMs.

Error example:
```
litellm.BadRequestError: OpenAIException - Error code: 400 - {
  'error': {
    'message': "Invalid schema for function 'stemmed_count_keywords_count_keywords_post_409a8e':
                In context=('properties', 'text'), array schema missing items.",
    'type': 'invalid_request_error',
    'param': 'tools[0].function.parameters',
    'code': 'invalid_function_parameters'
  }
}
```

The fix introduces simplify_union_schema() which:
- Detects Union types (anyOf/oneOf in OpenAPI)
- Selects the first non-null type from the union
- Preserves ALL properties including 'items' for arrays
- Maintains title, description, and default from parent schema

This ensures compatibility with LLMs that require strict JSON schema
validation for function parameters.

Fixes issues with common FastAPI patterns like:
- Union[str, List[str]] - single or multiple values
- Optional[List[T]] - optional arrays
- Union[str, List[Model]] - mixed type unions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant