JSON Schema Support

JSON Schemas are used in two places in this library:

Each provider supports a different subset of JSON Schema, and the library automatically sanitizes schemas for Google compatibility where possible.

Cross-Provider Compatibility#

The following JSON Schema features work across all providers (OpenAI, Anthropic, Google, Alibaba):

FeatureExample
type"type": "string"
properties"properties": {"name": {"type": "string"}}
required"required": ["name"]
description"description": "The user's name"
enum (strings)"enum": ["celsius", "fahrenheit"]
items (arrays)"items": {"type": "string"}
anyOf"anyOf": [{"type": "string"}, {"type": "integer"}]
default"default": "celsius"

Google Schema Sanitization#

Google Gemini does not support several JSON Schema features that OpenAI, Anthropic, and Alibaba accept. The library automatically handles these when using the Google provider:

FeatureBehavior
additionalProperties: falseSilently stripped
additionalProperties: trueRaises InvalidToolError
constConverted to single-value enum
allOfRaises InvalidToolError
["string", "null"] union typesConverted to "type": "string", "nullable": true
Non-string enum valuesRaises InvalidToolError

For schemas that raise InvalidToolError, you'll need to restructure them for Google compatibility. For example, instead of additionalProperties: true, consider using an array of key-value objects:

# Instead of this (not supported on Google): "metadata": { "type": "object", "additionalProperties": True } # Use this: "metadata": { "type": "array", "items": { "type": "object", "properties": { "key": {"type": "string"}, "value": {"type": "string"} }, "required": ["key", "value"] } }

Provider-Specific Differences#

FeatureOpenAIAnthropicGoogleAlibaba
additionalPropertiesRequired false in strict modeRequired false in strict modeNot supportedSame as OpenAI
minimum / maximumNot supportedSDK moves to descriptionSupportedNot supported
minLength / maxLengthNot supportedSDK moves to descriptionSupportedNot supported
patternNot supportedSupportedSupportedNot supported
formatNot supportedSupportedSupportedNot supported
constSupportedSupportedAuto-converted to enumSupported
allOfNot documentedSupportedNot supportedNot documented
nullable: trueUse ["type", "null"]Use ["type", "null"]Supported (Google-specific)Use ["type", "null"]
Non-string enumSupportedSupportedNot supportedSupported
Recursive schemasSupportedNot supportedLimitedSupported

Recommendations#

For maximum portability across providers, stick to:

  • Basic types: string, number, integer, boolean, object, array
  • properties, required, description, enum (strings only), items
  • Avoid additionalProperties, const, allOf, pattern, format, minimum/maximum
toolsschemasstructured-outputgoogle