Skip to content

Commit 2cb371c

Browse files
authored
Merge pull request #1 from adri22235/claude/python-genai-issues-d5gnhj
fix: address several open SDK bugs (googleapis#2937, googleapis#2938, googleapis#3007, googleapis#2991, googleapis#2995)
2 parents e6d3168 + 8263e86 commit 2cb371c

9 files changed

Lines changed: 253 additions & 7 deletions

File tree

‎google/genai/_api_client.py‎

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import sys
3535
import threading
3636
import time
37-
from typing import Any, AsyncIterator, Iterator, Optional, TYPE_CHECKING, Tuple, Union, cast, overload
37+
from typing import Any, AsyncIterator, Callable, Coroutine, Iterator, Optional, TYPE_CHECKING, Tuple, Union, cast, overload
3838
from urllib.parse import urlparse
3939
from urllib.parse import urlunparse
4040
import warnings
@@ -615,6 +615,38 @@ def __del__(self) -> None:
615615
pass
616616

617617

618+
# Strong references to close tasks scheduled from `__del__`, so they are not
619+
# garbage collected (and destroyed while pending) before they finish.
620+
_background_close_tasks: set['asyncio.Task[Any]'] = set()
621+
622+
623+
def _spawn_close_task(
624+
loop: asyncio.AbstractEventLoop, close: Callable[[], Coroutine[Any, Any, Any]]
625+
) -> None:
626+
"""Runs `close()` as a task on `loop`. Must be called from `loop`'s thread."""
627+
if loop.is_closed():
628+
return
629+
task: 'asyncio.Task[Any]' = loop.create_task(close())
630+
_background_close_tasks.add(task)
631+
task.add_done_callback(_background_close_tasks.discard)
632+
633+
634+
def _schedule_close_on_loop(
635+
loop: asyncio.AbstractEventLoop, close: Callable[[], Coroutine[Any, Any, Any]]
636+
) -> None:
637+
"""Schedules `close()` on `loop`, which may belong to another thread."""
638+
try:
639+
current_loop: Optional[asyncio.AbstractEventLoop] = (
640+
asyncio.get_running_loop()
641+
)
642+
except RuntimeError:
643+
current_loop = None
644+
if loop is current_loop:
645+
_spawn_close_task(loop, close)
646+
elif loop.is_running():
647+
loop.call_soon_threadsafe(_spawn_close_task, loop, close)
648+
649+
618650
class AsyncHttpxClient(httpx.AsyncClient):
619651
"""Async httpx client."""
620652

@@ -630,7 +662,7 @@ def __del__(self) -> None:
630662
except Exception:
631663
pass
632664
try:
633-
asyncio.get_running_loop().create_task(self.aclose())
665+
_spawn_close_task(asyncio.get_running_loop(), self.aclose)
634666
except Exception:
635667
pass
636668

@@ -2315,7 +2347,7 @@ async def _async_upload_fd(
23152347
break
23162348
delay_seconds = INITIAL_RETRY_DELAY * (DELAY_MULTIPLIER**retry_count)
23172349
retry_count += 1
2318-
time.sleep(delay_seconds)
2350+
await asyncio.sleep(delay_seconds)
23192351

23202352
offset += chunk_size
23212353
if (
@@ -2535,8 +2567,25 @@ def __del__(self) -> None:
25352567
except Exception: # pylint: disable=broad-except
25362568
pass
25372569

2570+
# Each aiohttp session must be closed on the event loop that created it,
2571+
# which is not necessarily the loop (if any) running in the thread that
2572+
# garbage collects this client.
2573+
try:
2574+
if (
2575+
not self._http_options.httpx_async_client
2576+
and self._async_httpx_client
2577+
):
2578+
_spawn_close_task(
2579+
asyncio.get_running_loop(), self._async_httpx_client.aclose
2580+
)
2581+
except Exception: # pylint: disable=broad-except
2582+
pass
2583+
25382584
try:
2539-
asyncio.get_running_loop().create_task(self.aclose())
2585+
if self._aiohttp_sessions and not self._http_options.aiohttp_client:
2586+
for loop, session in list(self._aiohttp_sessions.items()):
2587+
if not self._is_session_closed(session):
2588+
_schedule_close_on_loop(loop, session.close)
25402589
except Exception: # pylint: disable=broad-except
25412590
pass
25422591

‎google/genai/_common.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import logging
2424
import re
2525
import sys
26+
import threading
2627
import typing
2728
from typing import Any, Callable, FrozenSet, Optional, Union, get_args, get_origin
2829
import uuid
@@ -558,6 +559,12 @@ def _format_collection(
558559
return f'{brackets[0]}\n' + ',\n'.join(elements) + f',\n{indent}{brackets[1]}'
559560

560561

562+
# Pydantic's deferred model build (`defer_build=True` below) is not thread safe:
563+
# a thread that uses a model while another thread is building it can see a
564+
# half-built model. Builds are serialized through this lock.
565+
_MODEL_BUILD_LOCK = threading.RLock()
566+
567+
561568
class BaseModel(pydantic.BaseModel):
562569

563570
model_config = pydantic.ConfigDict(
@@ -578,6 +585,38 @@ class BaseModel(pydantic.BaseModel):
578585
defer_build=True,
579586
)
580587

588+
@classmethod
589+
def _ensure_model_built(cls) -> None:
590+
"""Builds the deferred validator and serializer, once, under a lock."""
591+
if cls.__pydantic_complete__:
592+
return
593+
with _MODEL_BUILD_LOCK:
594+
if not cls.__pydantic_complete__:
595+
cls.model_rebuild(raise_errors=False, _parent_namespace_depth=0)
596+
597+
if not typing.TYPE_CHECKING:
598+
# Hidden from type checkers so that they keep using the `__init__` and
599+
# `model_validate*` signatures that Pydantic synthesizes for each model.
600+
601+
def __init__(self, /, **data: Any) -> None:
602+
type(self)._ensure_model_built()
603+
super().__init__(**data)
604+
605+
@classmethod
606+
def model_validate(cls, *args: Any, **kwargs: Any) -> Any:
607+
cls._ensure_model_built()
608+
return super().model_validate(*args, **kwargs)
609+
610+
@classmethod
611+
def model_validate_json(cls, *args: Any, **kwargs: Any) -> Any:
612+
cls._ensure_model_built()
613+
return super().model_validate_json(*args, **kwargs)
614+
615+
@classmethod
616+
def model_validate_strings(cls, *args: Any, **kwargs: Any) -> Any:
617+
cls._ensure_model_built()
618+
return super().model_validate_strings(*args, **kwargs)
619+
581620
@pydantic.model_validator(mode='before')
582621
@classmethod
583622
def _check_field_type_mismatches(cls, data: Any) -> Any:

‎google/genai/_transformers.py‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -765,15 +765,31 @@ def process_schema(
765765
# After removing null fields, Optional fields with only one possible type
766766
# will have a $ref key that needs to be flattened
767767
# For example: {'default': None, 'description': 'Name of the person', 'nullable': True, '$ref': '#/$defs/TestPerson'}
768+
# Annotations next to the '$ref' (e.g. a field-level 'description') take
769+
# precedence over the ones on the referenced definition.
768770
if (ref := schema.pop('$ref', None)) is not None:
769-
schema.update(defs[ref.split('defs/')[-1]])
771+
for key, value in defs[ref.split('defs/')[-1]].items():
772+
schema.setdefault(key, value)
770773

771774
def _recurse(sub_schema: _common.StringDict) -> _common.StringDict:
772775
"""Returns the processed `sub_schema`, resolving its '$ref' if any."""
773776
if (ref := sub_schema.pop('$ref', None)) is not None:
774-
sub_schema = defs[ref.split('defs/')[-1]]
775-
if id(sub_schema) in visited_dicts_path:
777+
ref_schema: _common.StringDict = defs[ref.split('defs/')[-1]]
778+
if id(ref_schema) in visited_dicts_path:
776779
return {}
780+
process_schema(
781+
ref_schema,
782+
client,
783+
defs,
784+
order_properties=order_properties,
785+
visited_dicts_path=visited_dicts_path,
786+
)
787+
if not sub_schema:
788+
return ref_schema
789+
# Keep annotations next to the '$ref' (e.g. a field-level
790+
# 'description'), which would otherwise be replaced by the ones on the
791+
# shared definition.
792+
return {**ref_schema, **sub_schema}
777793

778794
process_schema(
779795
sub_schema,

‎google/genai/tests/client/test_client_close.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
"""Tests for closing the clients and context managers."""
1717
import asyncio
18+
import threading
1819
from unittest import mock
1920

2021
from google.oauth2 import credentials
@@ -220,3 +221,35 @@ async def run():
220221
assert async_client._api_client._aiohttp_session._auth_request._closed
221222

222223
asyncio.run(run())
224+
225+
226+
def test_del_closes_aiohttp_session_on_its_own_loop():
227+
"""Tests that `__del__` closes each aiohttp session on the loop that made it."""
228+
owner_loop = asyncio.new_event_loop()
229+
owner_thread = threading.Thread(target=owner_loop.run_forever, daemon=True)
230+
owner_thread.start()
231+
closed_on = []
232+
closed = threading.Event()
233+
234+
class _Session:
235+
closed = False
236+
237+
async def close(self):
238+
closed_on.append(asyncio.get_running_loop())
239+
closed.set()
240+
241+
client = api_client.BaseApiClient(api_key='test_api_key')
242+
client._aiohttp_sessions[owner_loop] = _Session()
243+
244+
async def collect_on_another_loop():
245+
client.__del__()
246+
await asyncio.sleep(0)
247+
248+
try:
249+
asyncio.run(collect_on_another_loop())
250+
assert closed.wait(timeout=5)
251+
assert closed_on == [owner_loop]
252+
finally:
253+
owner_loop.call_soon_threadsafe(owner_loop.stop)
254+
owner_thread.join(timeout=5)
255+
owner_loop.close()

‎google/genai/tests/client/test_upload_errors.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,33 @@ async def test_async_upload_fd_error_aiohttp(client: api_client.BaseApiClient):
195195
)
196196
assert mock_aiohttp_session.request.call_count == 2
197197

198+
199+
200+
@pytest.mark.asyncio
201+
async def test_async_upload_retry_does_not_block_event_loop(
202+
client: api_client.BaseApiClient,
203+
):
204+
mock_async_httpx_client = mock.MagicMock(spec=httpx.AsyncClient)
205+
mock_async_httpx_client.request = mock.AsyncMock(
206+
side_effect=[
207+
_httpx_response(200), # Missing upload status, so it is retried.
208+
_httpx_response(200, headers={"X-Goog-Upload-Status": "final"}),
209+
]
210+
)
211+
client._async_httpx_client = mock_async_httpx_client
212+
213+
with mock.patch.object(
214+
client, "_use_aiohttp", return_value=False
215+
), mock.patch.object(
216+
api_client.time, "sleep", side_effect=AssertionError("blocking sleep")
217+
), mock.patch.object(
218+
api_client.asyncio, "sleep", new_callable=mock.AsyncMock
219+
) as mock_async_sleep, io.BytesIO(
220+
b"test"
221+
) as f:
222+
await client._async_upload_fd(
223+
f, "http://fake/upload", 4, http_options=types.HttpOptions()
224+
)
225+
226+
assert mock_async_httpx_client.request.call_count == 2
227+
mock_async_sleep.assert_awaited_once()

‎google/genai/tests/common/test_common.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,3 +952,38 @@ class TestModel(_common.BaseModel):
952952
TestModel.model_validate(data)
953953

954954
assert len(caplog.records) == 0
955+
956+
957+
def test_deferred_model_build_is_thread_safe():
958+
import concurrent.futures
959+
import sys
960+
import threading
961+
962+
class _Inner(_common.BaseModel):
963+
value: Optional[int] = None
964+
965+
class _Outer(_common.BaseModel):
966+
max_output_tokens: Optional[int] = None
967+
seed: Optional[int] = None
968+
inner: Optional[_Inner] = None
969+
970+
assert not _Outer.__pydantic_complete__
971+
num_threads = 16
972+
barrier = threading.Barrier(num_threads)
973+
974+
def build():
975+
barrier.wait()
976+
return _Outer(max_output_tokens=32768, seed=42, inner={'value': 1})
977+
978+
switch_interval = sys.getswitchinterval()
979+
sys.setswitchinterval(1e-6)
980+
try:
981+
with concurrent.futures.ThreadPoolExecutor(num_threads) as executor:
982+
results = list(executor.map(lambda _: build(), range(num_threads)))
983+
finally:
984+
sys.setswitchinterval(switch_interval)
985+
986+
assert _Outer.__pydantic_complete__
987+
for result in results:
988+
assert result.max_output_tokens == 32768
989+
assert result.inner.value == 1

‎google/genai/tests/transformers/test_schema.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,3 +693,38 @@ def test_t_schema_sets_property_ordering_for_schema_type(client):
693693

694694
transformed_schema = _transformers.t_schema(client, schema)
695695
assert transformed_schema.property_ordering == ['name', 'population']
696+
697+
698+
class _SharedScore(pydantic.BaseModel):
699+
"""A generic score container."""
700+
701+
point: int
702+
703+
704+
class _ReviewWithSharedScores(pydantic.BaseModel):
705+
clarity: _SharedScore = pydantic.Field(description='Rate clarity only.')
706+
accuracy: _SharedScore = pydantic.Field(
707+
description='Rate factual accuracy only.'
708+
)
709+
optional_score: Union[_SharedScore, None] = pydantic.Field(
710+
default=None, description='An optional score.'
711+
)
712+
scores: list[_SharedScore]
713+
714+
715+
@pytest.mark.parametrize('use_vertex', [True, False])
716+
def test_t_schema_keeps_field_descriptions_of_referenced_models(client):
717+
transformed_schema = _transformers.t_schema(client, _ReviewWithSharedScores)
718+
719+
properties = transformed_schema.properties
720+
assert properties['clarity'].description == 'Rate clarity only.'
721+
assert properties['accuracy'].description == 'Rate factual accuracy only.'
722+
assert properties['optional_score'].description == 'An optional score.'
723+
assert properties['optional_score'].nullable
724+
# Without a field-level description, the model's docstring is used.
725+
assert (
726+
properties['scores'].items.description == 'A generic score container.'
727+
)
728+
for name in ('clarity', 'accuracy', 'optional_score'):
729+
assert properties[name].type == types.Type.OBJECT
730+
assert properties[name].properties['point'].type == types.Type.INTEGER

‎google/genai/tests/types/test_types.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2970,3 +2970,10 @@ def test_computer_use_types():
29702970
assert len(c.disabled_safety_policies) == 2
29712971
assert types.SafetyPolicy.FINANCIAL_TRANSACTIONS in c.disabled_safety_policies
29722972

2973+
2974+
2975+
def test_finish_reason_malformed_response():
2976+
candidate = types.Candidate.model_validate(
2977+
{'finishReason': 'MALFORMED_RESPONSE'}
2978+
)
2979+
assert candidate.finish_reason is types.FinishReason.MALFORMED_RESPONSE

‎google/genai/types.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,8 @@ class FinishReason(_common.CaseInSensitiveEnum):
513513
"""Token generation stopped because the content potentially contains Sensitive Personally Identifiable Information (SPII)."""
514514
MALFORMED_FUNCTION_CALL = 'MALFORMED_FUNCTION_CALL'
515515
"""The function call generated by the model is invalid."""
516+
MALFORMED_RESPONSE = 'MALFORMED_RESPONSE'
517+
"""The response generated by the model is malformed and could not be parsed."""
516518
IMAGE_SAFETY = 'IMAGE_SAFETY'
517519
"""Token generation stopped because generated images have safety violations."""
518520
UNEXPECTED_TOOL_CALL = 'UNEXPECTED_TOOL_CALL'

0 commit comments

Comments
 (0)