Skip to content

v2.18 defers building Pydantic models until first use, which causes errors in a multithreaded context #2991

Description

@jmazella-brr

Summary

  • Version 2.18 added defer_build=True for 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

  1. Import a config, eg from google.genai.types import GenerateContentConfig
  2. 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
        )

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

priority: p2Moderately-important priority. Fix may not be included in next release.status:awaiting user responsetype: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions