Skip to content

Commit cab0911

Browse files
conradleeclaude
andcommitted
Use different JSON schema transformers for GLA vs Vertex AI
Based on reviewer feedback, this commit implements different JSON schema transformation strategies for Google's two API endpoints: - GLA (Generative Language API): Uses GoogleGLAJsonSchemaTransformer which inlines $defs as a defensive measure for recursive schemas that have historically had reliability issues on GLA - Vertex AI: Uses GoogleJsonSchemaTransformer which preserves $refs and takes full advantage of Google's enhanced JSON Schema support This approach provides maximum compatibility while being defensive about potential issues with recursive schemas on GLA. Changes: - Added is_vertexai parameter to google_model_profile() - GoogleProvider.model_profile() now checks the client type and passes it along - Created GoogleGLAJsonSchemaTransformer that inlines definitions - Vertex AI continues to use the standard GoogleJsonSchemaTransformer 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 8dcf07a commit cab0911

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

pydantic_ai_slim/pydantic_ai/profiles/google.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,19 @@
44
from . import ModelProfile
55

66

7-
def google_model_profile(model_name: str) -> ModelProfile | None:
8-
"""Get the model profile for a Google model."""
7+
def google_model_profile(model_name: str, *, is_vertexai: bool = False) -> ModelProfile | None:
8+
"""Get the model profile for a Google model.
9+
10+
Args:
11+
model_name: The name of the model.
12+
is_vertexai: Whether this is a Vertex AI model (as opposed to GLA).
13+
"""
914
is_image_model = 'image' in model_name
15+
# For GLA, use the transformer that inlines defs (defensive approach for recursive schemas)
16+
# For Vertex AI, use the regular transformer with enhanced JSON Schema support
17+
transformer_class = GoogleJsonSchemaTransformer if is_vertexai else GoogleGLAJsonSchemaTransformer
1018
return ModelProfile(
11-
json_schema_transformer=GoogleJsonSchemaTransformer,
19+
json_schema_transformer=transformer_class,
1220
supports_image_output=is_image_model,
1321
supports_json_schema_output=not is_image_model,
1422
supports_json_object_output=not is_image_model,
@@ -44,3 +52,15 @@ def transform(self, schema: JsonSchema) -> JsonSchema:
4452
schema.pop('exclusiveMaximum', None)
4553

4654
return schema
55+
56+
57+
class GoogleGLAJsonSchemaTransformer(GoogleJsonSchemaTransformer):
58+
"""Transforms the JSON Schema for Google's Generative Language API (GLA).
59+
60+
This transformer inlines $defs as a defensive measure for recursive schemas,
61+
which have historically had reliability issues on GLA.
62+
"""
63+
64+
def __init__(self, schema: JsonSchema, *, strict: bool | None = None):
65+
# Initialize with prefer_inlined_defs=True to inline definitions
66+
super().__init__(schema, strict=strict, prefer_inlined_defs=True)

pydantic_ai_slim/pydantic_ai/providers/google.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ def client(self) -> Client:
3939
return self._client
4040

4141
def model_profile(self, model_name: str) -> ModelProfile | None:
42-
return google_model_profile(model_name)
42+
# Check if we're using Vertex AI or GLA
43+
is_vertexai = bool(self._client._api_client.vertexai) # type: ignore[reportPrivateUsage]
44+
return google_model_profile(model_name, is_vertexai=is_vertexai)
4345

4446
@overload
4547
def __init__(

0 commit comments

Comments
 (0)