Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.x] Upgrade to LangChain v0.3 and Pydantic v2 #1199

Merged
merged 12 commits into from
Jan 15, 2025
Merged

[2.x] Upgrade to LangChain v0.3 and Pydantic v2 #1199

merged 12 commits into from
Jan 15, 2025

Conversation

dlqqq
Copy link
Member

@dlqqq dlqqq commented Jan 14, 2025

Description

  • Closes support langchain>=0.3 and migrate to use pydantic v2 #1003.
  • Upgrades to langchain>=0.3.0,<0.4.0 and pydantic>=2.0,<3.0.
  • Implements code changes required by this upgrade. These are detailed below in separate sections.
  • Removes import-linter tool from the project, as it is no longer needed to block importing from pydantic directly.

cc @michaelchia @krassowski

Demo

Screen.Recording.2025-01-14.at.1.08.29.PM.mov

Notable Pydantic v2 changes

  • In BaseModel, the .dict() and .json() methods have been deprecated in favor of .model_dump() and .model_dump_json() respectively.

  • All Optional fields no longer default to a value of None in Pydantic v2. The default value of None must be explicitly set in the model definition, otherwise an exception is raised at runtime.

    • I used this regex to find all Optional fields lacking a default: Optional\[[\w\[\]\s,]*\]\s*$. I then added a default value of None to each.
  • It is no longer possible to implement an attribute from a parent class via a property while also inheriting from BaseModel. Upstream issue: Failure to override an inherited field with a computed field pydantic/pydantic#7250

    • Therefore, I've had to drop BaseModel from the BoundedChatHistory classes. They now just directly inherit from BaseChatMessageHistory.
  • The ModelMetaclass is no longer available from a public module, and should be considered for internal use by Pydantic only. Therefore this PR no longer subclasses ModelMetaclass and reverts LangChain v0.1.0 #572.

  • AFAIK, it is no longer possible to verify server_settings: ClassVar[MappingProxyType] is only set once. I've removed the associated test.

    • Previously this was implemented in the custom metaclass derived from ModelMetaclass used by Pydantic internally (Prevent overriding server_settings on base provider class #825). However, in Pydantic v2, this metaclass is not exported by a public module, and therefore should not be subclassed.
    • Model & field validators can't meet this use-case either, because ClassVar attributes are not treated as fields by Pydantic: no error when using ClassVar and Field pydantic/pydantic#10169
    • I believe the long term solution is to avoid using ClassVar attributes as much as possible. The behavior of ClassVars isn't as well-defined as the behavior of other fields. For example, instead of using ClassVars to track globals, we should probably just store them as instance fields on singleton objects, e.g. ConfigManager.

Notable LangChain changes

Surprisingly, I didn't notice any which impact Jupyter AI. Reviewers, please let me know if I missed something.

For reviewers

  • Please test the application as much as possible. Both Pydantic & LangChain are used pervasively throughout the codebase, and both have introduced breaking changes since the previous version set.

  • What I've tested:

    • I can change my chat & embedding model freely. Haven't tested more advanced scenarios such as typing a custom model ID or adding model fields.
    • Chatting with OpenAI & Anthropic work as expected.
    • All slash commands work as expected.
    • Stopping an AI stream response works as expected.

@dlqqq dlqqq added the enhancement New feature or request label Jan 14, 2025
@dlqqq dlqqq added this to the v2.29.0 milestone Jan 14, 2025
dlqqq added a commit to dlqqq/jupyter-ai that referenced this pull request Jan 14, 2025
remove importliner from project

initial upgrade to langchain~=0.3, pydantic~=2.0

default to `None` for all `Optional` fields explicitly

fix history impl for Pydantic v2, fixes chat

prefer `.model_dump_json()` over `.json()`

Addresses a Pydantic v2 deprecation warning, as `BaseModel.json()` is
now deprecated in favor of `BaseModel.model_dump_json()`.

replace `.dict()` with `.model_dump()`.

`BaseModel.dict()` is deprecated in favor of `BaseModel.model_dump()` in
Pydantic v2.

fix BaseProvider.server_settings

fix OpenRouterProvider

fix remaining unit tests

address all Pydantic v1 deprecation warnings

pre-commit

fix mypy
@dlqqq
Copy link
Member Author

dlqqq commented Jan 14, 2025

Ah, I forgot to update the docs. I'll open a new issue to track this for fast follow-up, since I've already ported these changes to the main branch in #1201.

New issue: #1200.

@dlqqq
Copy link
Member Author

dlqqq commented Jan 15, 2025

Hey folks, our team of 5 engineers performed a 30 minute bug bash on this PR and didn't find any new bugs or regressions. Therefore, I will merge this PR into 2.x and include this in the v2.29.0 release. We are hoping to publish this release tomorrow. 👍

@dlqqq dlqqq merged commit 26593dc into 2.x Jan 15, 2025
11 checks passed
@dlqqq dlqqq deleted the v2-langchain-030 branch January 15, 2025 22:12
dlqqq added a commit that referenced this pull request Jan 15, 2025
* port PR #1199 to `main` branch, tracking v3

remove importliner from project

initial upgrade to langchain~=0.3, pydantic~=2.0

default to `None` for all `Optional` fields explicitly

fix history impl for Pydantic v2, fixes chat

prefer `.model_dump_json()` over `.json()`

Addresses a Pydantic v2 deprecation warning, as `BaseModel.json()` is
now deprecated in favor of `BaseModel.model_dump_json()`.

replace `.dict()` with `.model_dump()`.

`BaseModel.dict()` is deprecated in favor of `BaseModel.model_dump()` in
Pydantic v2.

fix BaseProvider.server_settings

fix OpenRouterProvider

fix remaining unit tests

address all Pydantic v1 deprecation warnings

pre-commit

fix mypy

* remove stray pydantic v1 import
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant