langchain-ai / langchain

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

Can't import ChatOpenAI: The `__modify_schema__` method is not supported in Pydantic v2. #28255

Open ZmeiGorynych opened 1 day ago

ZmeiGorynych commented 1 day ago

Checked other resources

Example Code

The following code straight from the docs fails already at the import statement:

from typing import Optional

from pydantic import BaseModel, Field

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(model="gpt-4o-mini")

# Pydantic
class Joke(BaseModel):
    """Joke to tell user."""

    setup: str = Field(description="The setup of the joke")
    punchline: str = Field(description="The punchline to the joke")
    rating: Optional[int] = Field(default=None, description="How funny the joke is, from 1 to 10")

structured_llm = llm.with_structured_output(Joke)

structured_llm.invoke("Tell me a joke about cats")

Error Message and Stack Trace (if applicable)

C:\Users\Egor\Dropbox\Code\langchain\libs\partners\openai\langchain_openai\chat_models\__init__.py:1: LangChainDeprecationWarning: As of langchain-core 0.3.0, LangChain uses pydantic v2 internally. The langchain_core.pydantic_v1 module was a compatibility shim for pydantic v1, and should no longer be used. Please update the code to import from Pydantic directly.

C:\Users\Egor\Dropbox\Code\langchain\libs\partners\openai\langchain_openai\chat_models\__init__.py:1: LangChainDeprecationWarning: As of langchain-core 0.3.0, LangChain uses pydantic v2 internally. The langchain_core.pydantic_v1 module was a compatibility shim for pydantic v1, and should no longer be used. Please update the code to import from Pydantic directly.

For example, replace imports like: `from langchain_core.pydantic_v1 import BaseModel`
with: `from pydantic import BaseModel`
or the v1 compatibility namespace if you are working in a code base that has not been fully upgraded to pydantic 2 yet.     from pydantic.v1 import BaseModel

  from langchain_openai.chat_models.azure import AzureChatOpenAI
C:\Users\Egor\.conda\envs\motleycrew3.11\Lib\site-packages\pydantic\_internal\_config.py:345: UserWarning: Valid config keys have changed in V2:
* 'allow_population_by_field_name' has been renamed to 'populate_by_name'
  warnings.warn(message, UserWarning)
Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "C:\Users\Egor\Dropbox\Code\langchain\libs\partners\openai\langchain_openai\__init__.py", line 1, in <module>
    from langchain_openai.chat_models import AzureChatOpenAI, ChatOpenAI
  File "C:\Users\Egor\Dropbox\Code\langchain\libs\partners\openai\langchain_openai\chat_models\__init__.py", line 1, in <module>
    from langchain_openai.chat_models.azure import AzureChatOpenAI
  File "C:\Users\Egor\Dropbox\Code\langchain\libs\partners\openai\langchain_openai\chat_models\azure.py", line 41, in <module>
    from langchain_openai.chat_models.base import BaseChatOpenAI
  File "C:\Users\Egor\Dropbox\Code\langchain\libs\partners\openai\langchain_openai\chat_models\base.py", line 353, in <module>
    class BaseChatOpenAI(BaseChatModel):
  File "C:\Users\Egor\.conda\envs\motleycrew3.11\Lib\site-packages\pydantic\_internal\_model_construction.py", line 226, in __new__
    complete_model_class(
  File "C:\Users\Egor\.conda\envs\motleycrew3.11\Lib\site-packages\pydantic\_internal\_model_construction.py", line 658, in complete_model_class
    schema = cls.__get_pydantic_core_schema__(cls, handler)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Egor\.conda\envs\motleycrew3.11\Lib\site-packages\pydantic\main.py", line 697, in __get_pydantic_core_schema__
    return handler(source)
           ^^^^^^^^^^^^^^^
  File "C:\Users\Egor\.conda\envs\motleycrew3.11\Lib\site-packages\pydantic\_internal\_schema_generation_shared.py", line 84, in __call__
    schema = self._handler(source_type)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Egor\.conda\envs\motleycrew3.11\Lib\site-packages\pydantic\_internal\_generate_schema.py", line 612, in generate_schema
    schema = self._generate_schema_inner(obj)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Egor\.conda\envs\motleycrew3.11\Lib\site-packages\pydantic\_internal\_generate_schema.py", line 881, in _generate_schema_inner
    return self._model_schema(obj)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Egor\.conda\envs\motleycrew3.11\Lib\site-packages\pydantic\_internal\_generate_schema.py", line 693, in _model_schema
    {k: self._generate_md_field_schema(k, v, decorators) for k, v in fields.items()},
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Egor\.conda\envs\motleycrew3.11\Lib\site-packages\pydantic\_internal\_generate_schema.py", line 693, in <dictcomp>
    {k: self._generate_md_field_schema(k, v, decorators) for k, v in fields.items()},
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Egor\.conda\envs\motleycrew3.11\Lib\site-packages\pydantic\_internal\_generate_schema.py", line 1073, in _generate_md_field_schema
    common_field = self._common_field_schema(name, field_info, decorators)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Egor\.conda\envs\motleycrew3.11\Lib\site-packages\pydantic\_internal\_generate_schema.py", line 1261, in _common_field_schema
    schema = self._apply_annotations(
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Egor\.conda\envs\motleycrew3.11\Lib\site-packages\pydantic\_internal\_generate_schema.py", line 2051, in _apply_annotations
    schema = get_inner_schema(source_type)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Egor\.conda\envs\motleycrew3.11\Lib\site-packages\pydantic\_internal\_schema_generation_shared.py", line 84, in __call__
    schema = self._handler(source_type)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Egor\.conda\envs\motleycrew3.11\Lib\site-packages\pydantic\_internal\_generate_schema.py", line 2032, in inner_handler
    schema = self._generate_schema_inner(obj)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Egor\.conda\envs\motleycrew3.11\Lib\site-packages\pydantic\_internal\_generate_schema.py", line 886, in _generate_schema_inner
    return self.match_type(obj)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Egor\.conda\envs\motleycrew3.11\Lib\site-packages\pydantic\_internal\_generate_schema.py", line 988, in match_type
    return self._match_generic_type(obj, origin)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Egor\.conda\envs\motleycrew3.11\Lib\site-packages\pydantic\_internal\_generate_schema.py", line 1016, in _match_generic_type
    return self._union_schema(obj)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Egor\.conda\envs\motleycrew3.11\Lib\site-packages\pydantic\_internal\_generate_schema.py", line 1323, in _union_schema
    choices.append(self.generate_schema(arg))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Egor\.conda\envs\motleycrew3.11\Lib\site-packages\pydantic\_internal\_generate_schema.py", line 614, in generate_schema
    metadata_js_function = _extract_get_pydantic_json_schema(obj, schema)
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Egor\.conda\envs\motleycrew3.11\Lib\site-packages\pydantic\_internal\_generate_schema.py", line 2384, in _extract_get_pydantic_json_schema
    raise PydanticUserError(
pydantic.errors.PydanticUserError: The `__modify_schema__` method is not supported in Pydantic v2. Use `__get_pydantic_json_schema__` instead in class `SecretStr`.

For further information visit https://errors.pydantic.dev/2.10/u/custom-json-schema

Process finished with exit code 1

Description

Trying to use the latest langchain_openai,

System Info


System Information
------------------
> OS:  Windows
> OS Version:  10.0.19045
> Python Version:  3.10.15 | packaged by Anaconda, Inc. | (main, Oct  3 2024, 07:22:19) [MSC v.1929 64 bit (AMD64)]

Package Information
-------------------
> langchain_core: 0.3.19
> langchain: 0.3.7
> langchain_community: 0.3.7
> langsmith: 0.1.144
> langchain_experimental: 0.3.3
> langchain_openai: 0.2.9
> langchain_text_splitters: 0.3.2

Optional packages not installed
-------------------------------
> langgraph
> langserve

Other Dependencies
------------------
> aiohttp: 3.11.6
> async-timeout: 4.0.3
> dataclasses-json: 0.6.7
> httpx: 0.27.2
> httpx-sse: 0.4.0
> jsonpatch: 1.33
> numpy: 1.26.4
> openai: 1.55.0
> orjson: 3.10.11
> packaging: 24.2
> pydantic: 2.10.0
> pydantic-settings: 2.6.1
> PyYAML: 6.0.2
> requests: 2.32.3
> requests-toolbelt: 1.0.0
> SQLAlchemy: 2.0.35
> tenacity: 9.0.0
> tiktoken: 0.8.0
> typing-extensions: 4.12.2
suifengfengye commented 15 hours ago

I can run your code normally. Please use conda to create a new environment and try again.

EgorKraevTransferwise commented 9 hours ago

My apologies, the problem was indeed in my setup. I had indeed ran the code in a fresh environment, but had overlooked langchain source code that the IDE was loading instead of the installed packages. With that deleted, all was resolved. Thanks a lot for taking a look!