66
77from typing import Literal
88
9- import pytest
109from pydantic import BaseModel , Field
1110
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-
1711
1812class Cat (BaseModel ):
1913 """A cat."""
@@ -36,23 +30,40 @@ class Owner(BaseModel):
3630 pet : Cat | Dog = Field (..., discriminator = 'pet_type' )
3731
3832
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.
33+ async def test_discriminated_union_schema_stripping ():
34+ """Verify that discriminator field is stripped from schemas.
4435
4536 This test documents that while oneOf is supported, the discriminator field
46- used by Pydantic discriminated unions is not supported and causes validation errors.
37+ used by Pydantic discriminated unions must be stripped because it causes
38+ validation errors with Google's SDK.
4739
48- Expected error :
40+ Without stripping, we would get :
4941 properties.pet.oneOf: Extra inputs are not permitted
5042 """
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'
43+ from typing import Any
44+
45+ from pydantic_ai .profiles .google import GoogleJsonSchemaTransformer
46+
47+ # Generate schema for discriminated union
48+ schema = Owner .model_json_schema ()
49+
50+ # The schema should have discriminator before transformation
51+ assert 'discriminator' in schema ['$defs' ]['Owner' ]['properties' ]['pet' ]
52+
53+ # Transform the schema
54+ transformer = GoogleJsonSchemaTransformer (schema )
55+ transformed = transformer .walk ()
56+
57+ # Verify discriminator is stripped from all nested schemas
58+ def check_no_discriminator (obj : dict [str , Any ]) -> None :
59+ if isinstance (obj , dict ):
60+ assert 'discriminator' not in obj , 'discriminator should be stripped'
61+ for value in obj .values ():
62+ if isinstance (value , dict ):
63+ check_no_discriminator (value ) # type: ignore[arg-type]
64+ elif isinstance (value , list ):
65+ for item in value : # type: ignore[reportUnknownVariableType]
66+ if isinstance (item , dict ):
67+ check_no_discriminator (item ) # type: ignore[arg-type]
68+
69+ check_no_discriminator (transformed )
0 commit comments