Repository navigation
Open
Labels
priority: p2Moderately-important priority. Fix may not be included in next release.Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Description
Summary
- Version 2.18 added
defer_build=Truefor Pydantic validator models so the models are built on first use, not first import - Pydantic
model_build()is not thread safe - When the first use of these models is in a threaded context, there is a race condition where the first thread triggers the model build and waits as other threads continue to execute. The other threads then trigger Pydantic errors because the model is not ready. Example error:
Extra inputs are not permitted [type=extra_forbidden, input_value=1, input_type=int]
For further information visit https://errors.pydantic.dev/2.13/v/extra_forbidden
max_output_tokens
Traceback (most recent call last):
<original caller omitted>
File "/venv/lib/python3.12/site-packages/langchain_core/runnables/base.py", line 3442, in invoke
input_ = context.run(step.invoke, input_, config, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/langchain_core/runnables/base.py", line 4175, in invoke
key: future.result()
^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/concurrent/futures/_base.py", line 456, in result
return self.__get_result()
^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/concurrent/futures/_base.py", line 401, in __get_result
raise self._exception
File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 59, in run
result = self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/langchain_core/runnables/base.py", line 4158, in _invoke_step
return context.run(
^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/langchain_core/runnables/base.py", line 3444, in invoke
input_ = context.run(step.invoke, input_, config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/langchain_google_genai/chat_models.py", line 2791, in invoke
return super().invoke(input, config, stop=stop, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/langchain_core/language_models/chat_models.py", line 476, in invoke
self.generate_prompt(
File "/venv/lib/python3.12/site-packages/langchain_core/language_models/chat_models.py", line 1849, in generate_prompt
return self.generate(prompt_messages, stop=stop, callbacks=callbacks, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/langchain_core/language_models/chat_models.py", line 1656, in generate
self._generate_with_cache(
File "/venv/lib/python3.12/site-packages/langchain_core/language_models/chat_models.py", line 1998, in _generate_with_cache
result = self._generate(messages, stop=stop, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/brr/brr/llm/client/llms/google_genai.py", line 77, in _generate
return retry_decorator(super()._generate)(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/tenacity/__init__.py", line 289, in wrapped_f
return self(f, *args, **kw)
^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/tenacity/__init__.py", line 379, in __call__
do = self.iter(retry_state=retry_state)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/tenacity/__init__.py", line 314, in iter
return fut.result()
^^^^^^^^^^^^
File "/usr/local/lib/python3.12/concurrent/futures/_base.py", line 449, in result
return self.__get_result()
^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/concurrent/futures/_base.py", line 401, in __get_result
raise self._exception
File "/venv/lib/python3.12/site-packages/tenacity/__init__.py", line 382, in __call__
result = fn(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/langchain_google_genai/chat_models.py", line 3366, in _generate
request = self._prepare_request(
^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/langchain_google_genai/chat_models.py", line 3092, in _prepare_request
params: GenerationConfig = self._prepare_params(
^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/langchain_google_genai/chat_models.py", line 2835, in _prepare_params
return GenerationConfig.model_validate(gen_config)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/venv/lib/python3.12/site-packages/pydantic/main.py", line 732, in model_validate
return cls.__pydantic_validator__.validate_python(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Environment details
- Programming language: Python
- OS: All
- Language runtime version: 3.12
- Package version: >=2.18
Steps to reproduce
- Import a config, eg
from google.genai.types import GenerateContentConfig - Immediately spawn many threads that create new instances of GenerateContentConfig
Toy example:
def generate(model, contents):
generate_content(GenerateContentConfig(max_output_tokens=32768, seed=42), model, contents)
from google.genai.types import GenerateContentConfig
with ThreadPoolExecutor(max_workers=max_workers) as executor:
for model, contents in work_list:
executor.submit(
generate, model, contents
)
Metadata
Metadata
Assignees
Labels
priority: p2Moderately-important priority. Fix may not be included in next release.Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Activity
Venkaiahbabuneelam commentedon Sep 22, 2026
Hi @jmazella-brr, Thanks for reaching out us!
You've accurately identified a race condition stemming from Pydantic v2's deferred model building (
defer_build=True) in a multithreaded environment, wheremodel_build()is not thread-safe. This causes validation errors when multiple threads attempt to initializeGenerateContentConfigconcurrently for the first time.We tested the multithreaded scenario locally and we didn't encouner any errors, Please check and let us know if it resolves the issue for you.
If issue is still presist can you please provide me full reproducible code for reference.
Thanks
jmazella-brr commentedon Sep 24, 2026
@Venkaiahbabuneelam Yes I am still seeing the problem. Here is a self contained test script that shows the issue:
Here's the output i saw running it :
Venkaiahbabuneelam commentedon Sep 25, 2026
Hi @jmazella-brr, Thank you for bringing this to our attention. We truly appreciate you flagging this issue.
We are forwarding this issue to Our engineering team will investigate the thread-safety race condition caused by deferred Pydantic model building (defer_build=True) in multithreaded environments. We will update you here as soon as possible.
Thanks
Merge pull request #1 from adri22235/claude/python-genai-issues-d5gnhj
fix: Serialize deferred Pydantic model builds to fix a multithreaded …
fix: Serialize deferred Pydantic model builds to fix a multithreaded …
fix: Serialize deferred Pydantic model builds to fix a multithreaded …