langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
89.65k stars 14.17k forks source link

Python 3.12.4 is incompatible with pydantic.v1 as of pydantic==2.7.3 #22692

Open znruss opened 1 month ago

znruss commented 1 month ago

Checked other resources

Example Code

from langchain.schema import AIMessage

Error Message and Stack Trace (if applicable)

Traceback (most recent call last):
  File "/code/test.py", line 1, in <module>
    from langchain.schema import AIMessage
  File "/usr/local/lib/python3.12/site-packages/langchain/schema/__init__.py", line 5, in <module>
    from langchain_core.documents import BaseDocumentTransformer, Document
  File "/usr/local/lib/python3.12/site-packages/langchain_core/documents/__init__.py", line 6, in <module>
    from langchain_core.documents.compressor import BaseDocumentCompressor
  File "/usr/local/lib/python3.12/site-packages/langchain_core/documents/compressor.py", line 6, in <module>
    from langchain_core.callbacks import Callbacks
  File "/usr/local/lib/python3.12/site-packages/langchain_core/callbacks/__init__.py", line 22, in <module>
    from langchain_core.callbacks.manager import (
  File "/usr/local/lib/python3.12/site-packages/langchain_core/callbacks/manager.py", line 29, in <module>
    from langsmith.run_helpers import get_run_tree_context
  File "/usr/local/lib/python3.12/site-packages/langsmith/run_helpers.py", line 40, in <module>
    from langsmith import client as ls_client
  File "/usr/local/lib/python3.12/site-packages/langsmith/client.py", line 52, in <module>
    from langsmith import env as ls_env
  File "/usr/local/lib/python3.12/site-packages/langsmith/env/__init__.py", line 3, in <module>
    from langsmith.env._runtime_env import (
  File "/usr/local/lib/python3.12/site-packages/langsmith/env/_runtime_env.py", line 10, in <module>
    from langsmith.utils import get_docker_compose_command
  File "/usr/local/lib/python3.12/site-packages/langsmith/utils.py", line 31, in <module>
    from langsmith import schemas as ls_schemas
  File "/usr/local/lib/python3.12/site-packages/langsmith/schemas.py", line 69, in <module>
    class Example(ExampleBase):
  File "/usr/local/lib/python3.12/site-packages/pydantic/v1/main.py", line 286, in __new__
    cls.__try_update_forward_refs__()
  File "/usr/local/lib/python3.12/site-packages/pydantic/v1/main.py", line 807, in __try_update_forward_refs__
    update_model_forward_refs(cls, cls.__fields__.values(), cls.__config__.json_encoders, localns, (NameError,))
  File "/usr/local/lib/python3.12/site-packages/pydantic/v1/typing.py", line 554, in update_model_forward_refs
    update_field_forward_refs(f, globalns=globalns, localns=localns)
  File "/usr/local/lib/python3.12/site-packages/pydantic/v1/typing.py", line 520, in update_field_forward_refs
    field.type_ = evaluate_forwardref(field.type_, globalns, localns or None)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/pydantic/v1/typing.py", line 66, in evaluate_forwardref
    return cast(Any, type_)._evaluate(globalns, localns, set())
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: ForwardRef._evaluate() missing 1 required keyword-only argument: 'recursive_guard'

Description

Langchain fails on import with Python 3.12.4 due to pydantic v1 dependency. Python 3.12.3 is fine.

See https://github.com/pydantic/pydantic/issues/9607 for more info.

System Info

langchain                0.2.3
langchain-community      0.2.3
langchain-core           0.2.5
langchain-openai         0.1.8
pydantic                 2.7.3
pydantic_core            2.18.4

Python version is 3.12.4

Linux Arm64/v8

tarvindersi commented 1 month ago

I am using Python 3.12.3 and still having the same issue.

vkadel1111 commented 1 month ago

I am using Python 3.12.3 and still having the same issue.

I see the same error with 3.12.3 I'm using the 3.12-slim-bullseye. Is there any workaround?

znruss commented 1 month ago

Interesting. Check that your pydantic/pydantic core is the same version (2.7.3). For base image I'm using 3.12.3-slim arm64v8. Otherwise Pydantic seem to do weekly patches so perhaps (2.7.4) will fix it for you when it comes out. This guy had an additional insight. https://github.com/pydantic/pydantic/pull/9612#issuecomment-2158399679

tarvindersi commented 1 month ago

I am on pydantic 2.7.3. Thank you very much for your reply and pointers. Fingers crossed. I tried downloading a requirements.txt file from a published project and matched everything to it but no luck. It is possible they were using different python version (other than 3.12.3).

dennicLiu commented 1 month ago

The issue still persists.

housejester commented 1 month ago

I am using Python 3.12.3 and still having the same issue.

I see the same error with 3.12.3 I'm using the 3.12-slim-bullseye. Is there any workaround?

I believe the docker images for 3.12-slim-* got bumped to using 3.12.4 a few days ago. Pinning to 3.12.3-slim worked for me.

namiraprita commented 1 month ago

I'm also using Python 3.12.3 and still having the same issue.

znruss commented 1 month ago

I am using Python 3.12.3 and still having the same issue.

I see the same error with 3.12.3 I'm using the 3.12-slim-bullseye. Is there any workaround?

I believe the docker images for 3.12-slim-* got bumped to using 3.12.4 a few days ago. Pinning to 3.12.3-slim worked for me.

Correct - that's how we found this problem. We solved it by pinning the base docker image to 3.12.3-slim as well.

gbaian10 commented 1 month ago

Update pydantic to 2.7.4 will solve the problem.