Skip to content

Commit 083b369

Browse files
conradleeclaude
andcommitted
Document discriminator field limitation and add test
- Updated GoogleJsonSchemaTransformer docstring to note that discriminator is not supported (causes validation errors with nested oneOf) - Added reference to Google's announcement blog post - Added test_google_discriminator.py to document the limitation 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent f5911d3 commit 083b369

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

pydantic_ai_slim/pydantic_ai/profiles/google.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class GoogleJsonSchemaTransformer(JsonSchemaTransformer):
2121
2222
Gemini supports [a subset of OpenAPI v3.0.3](https://ai.google.dev/gemini-api/docs/function-calling#function_declarations).
2323
24-
As of November 2025, Gemini 2.5+ models support enhanced JSON Schema features including:
24+
As of November 2025, Gemini 2.5+ models support enhanced JSON Schema features
25+
(see [announcement](https://blog.google/technology/developers/gemini-api-structured-outputs/)) including:
2526
* `title` for short property descriptions
2627
* `anyOf` and `oneOf` for conditional structures (unions)
2728
* `$ref` and `$defs` for recursive schemas and reusable definitions
@@ -30,7 +31,9 @@ class GoogleJsonSchemaTransformer(JsonSchemaTransformer):
3031
* `type: 'null'` for optional fields
3132
* `prefixItems` for tuple-like arrays
3233
33-
Note: `exclusiveMinimum` and `exclusiveMaximum` are not yet supported by the Google SDK.
34+
Not supported (empirically tested as of November 2025):
35+
* `exclusiveMinimum` and `exclusiveMaximum` are not yet supported by the Google SDK
36+
* `discriminator` field causes validation errors with nested oneOf schemas
3437
"""
3538

3639
def __init__(self, schema: JsonSchema, *, strict: bool | None = None):
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""Test to verify that discriminator field is not supported by Google Gemini API.
2+
3+
This test empirically demonstrates that Pydantic discriminated unions (which generate
4+
oneOf schemas with discriminator mappings) cause validation errors with Google's SDK.
5+
"""
6+
7+
from typing import Literal
8+
9+
import pytest
10+
from pydantic import BaseModel, Field
11+
12+
from pydantic_ai import Agent
13+
from pydantic_ai.models.google import GoogleModel
14+
from pydantic_ai.output import NativeOutput
15+
from pydantic_ai.providers.google import GoogleProvider
16+
17+
18+
class Cat(BaseModel):
19+
"""A cat."""
20+
21+
pet_type: Literal['cat']
22+
meows: int
23+
24+
25+
class Dog(BaseModel):
26+
"""A dog."""
27+
28+
pet_type: Literal['dog']
29+
barks: float
30+
31+
32+
class Owner(BaseModel):
33+
"""An owner with a pet."""
34+
35+
name: str
36+
pet: Cat | Dog = Field(..., discriminator='pet_type')
37+
38+
39+
@pytest.mark.skip(
40+
reason='Discriminated unions (oneOf with discriminator) are not supported by Google Gemini API'
41+
)
42+
async def test_discriminated_union_not_supported():
43+
"""Verify that discriminated unions cause validation errors.
44+
45+
This test documents that while oneOf is supported, the discriminator field
46+
used by Pydantic discriminated unions is not supported and causes validation errors.
47+
48+
Expected error:
49+
properties.pet.oneOf: Extra inputs are not permitted
50+
"""
51+
provider = GoogleProvider(vertexai=True, project='ck-nest-dev', location='europe-west1')
52+
model = GoogleModel('gemini-2.5-flash', provider=provider)
53+
agent = Agent(model, output_type=NativeOutput(Owner))
54+
55+
# This would fail with validation error if discriminator was included
56+
result = await agent.run('Create an owner named John with a cat that meows 5 times')
57+
assert result.output.name == 'John'
58+
assert result.output.pet.pet_type == 'cat'

0 commit comments

Comments
 (0)