Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions google/genai/tests/types/test_schema_from_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,32 @@ def test_nullable_conversion():
assert set(vertex_ai_not_none_field_names2) == set(['type', 'nullable'])


def test_nullable_any_of_preserves_schema_metadata():
"""Test that nullable any_of conversion preserves schema metadata."""
json_schema = types.JSONSchema(
any_of=[
types.JSONSchema(type='string'),
types.JSONSchema(type='null'),
],
description='description',
title='title',
)

gemini_api_schema = types.Schema.from_json_schema(json_schema=json_schema)
vertex_ai_schema = types.Schema.from_json_schema(
json_schema=json_schema, api_option='VERTEX_AI'
)
expected_schema = types.Schema(
type='STRING',
nullable=True,
description='description',
title='title',
)

assert gemini_api_schema == expected_schema
assert vertex_ai_schema == expected_schema


def test_nullable_in_union_like_type_conversion():
"""Test conversion of JSONSchema.nullable to Schema.nullable"""
json_schema1 = types.JSONSchema(
Expand Down
6 changes: 6 additions & 0 deletions google/genai/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3470,11 +3470,17 @@ def convert_json_schema(
# If we found both parts, unwrap them into a single schema.
if nullable_part and type_part:
default_value = schema.default
description_value = schema.description
title_value = schema.title
schema = type_part
schema.nullable = True
# Carry the default value over to the unwrapped schema
if default_value is not None:
schema.default = default_value
if description_value is not None:
schema.description = description_value
if title_value is not None:
schema.title = title_value

if ref:
visited_refs.remove(ref)
Expand Down
Loading