[PR #3017] upgrade pydantic to 2.* #23958

Closed
opened 2026-02-21 20:22:05 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/3017

State: closed
Merged: No


Description

pydantic 2 introduced multiple enhancement, faster performance, and it was released 9 months ago, it's time to move forward and get better performance, more features.

summary of the changes:

  1. upgrade langchain, inference-client qdrant-client to the latest, add pydantic-extra-types as it's a new package now
  2. apply changes due to pydantic breaking changes, using bump-pydantic api
  3. fix a few type errors brought by YAML parse, those errors should exist before the upgrade, the strong type check by pydantic 2 exposed them.
  4. rename model_config field in AdvancedChatMessageEntity, MultiDatasetRouterAgent, AgentConfiguration to ai_model_config because model_config is reserved by pydantic 2

This is a big update and could be harmful, I advise someone else set up a coverage report like codecov, then I can see which part of the code is not covered by tests and add more tests to bring us more confidence.

Type of Change

Please delete options that are not relevant.

  • Dependency upgrade

How Has This Been Tested?

  • Start a server that works
@LeoQuote ➜ /workspaces/dify/api (upgrade_pydantic) $ FLASK_APP=app.py flask run 
/home/vscode/.local/lib/python3.10/site-packages/pydantic/_internal/_fields.py:151: UserWarning: Field "model_type" has conflict with protected namespace "model_".

You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`.
  warnings.warn(
None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
/home/vscode/.local/lib/python3.10/site-packages/pydub/utils.py:170: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work
  warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)
/home/vscode/.local/lib/python3.10/site-packages/langchain/llms/__init__.py:548: LangChainDeprecationWarning: Importing LLMs from langchain is deprecated. Importing from langchain will no longer be supported as of langchain==0.2.0. Please import from langchain-community instead:

`from langchain_community.llms import Tongyi`.

To install langchain-community run `pip install -U langchain-community`.
  warnings.warn(
/home/vscode/.local/lib/python3.10/site-packages/pydantic/_internal/_fields.py:151: UserWarning: Field "model_type_instance" has conflict with protected namespace "model_".

You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`.
  warnings.warn(
/home/vscode/.local/lib/python3.10/site-packages/pydantic/_internal/_fields.py:151: UserWarning: Field "model_schema" has conflict with protected namespace "model_".

You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`.
  warnings.warn(
WARNING:root:Missing subclass of ExternalDataTool in /workspaces/dify/api/core/external_data_tool/api/api.py, Skip.
/home/vscode/.local/lib/python3.10/site-packages/pydantic/_internal/_fields.py:186: UserWarning: Field name "raise_error" shadows an attribute in parent "BaseCallbackHandler"; 
  warnings.warn(
/home/vscode/.local/lib/python3.10/site-packages/pydantic/_internal/_fields.py:186: UserWarning: Field name "run_inline" shadows an attribute in parent "BaseCallbackHandler"; 
  warnings.warn(
/home/vscode/.local/lib/python3.10/site-packages/pydantic/_internal/_fields.py:151: UserWarning: Field "model_configuration" has conflict with protected namespace "model_".

You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`.
  warnings.warn(
/home/vscode/.local/lib/python3.10/site-packages/langchain/__init__.py:29: UserWarning: Importing BasePromptTemplate from langchain root module is no longer supported. Please use langchain_core.prompts.BasePromptTemplate instead.
  warnings.warn(
/home/vscode/.local/lib/python3.10/site-packages/langchain/__init__.py:29: UserWarning: Importing PromptTemplate from langchain root module is no longer supported. Please use langchain_core.prompts.PromptTemplate instead.
  warnings.warn(
/home/vscode/.local/lib/python3.10/site-packages/langchain/__init__.py:29: UserWarning: Importing LLMChain from langchain root module is no longer supported. Please use langchain.chains.LLMChain instead.
  warnings.warn(
/home/vscode/.local/lib/python3.10/site-packages/pydantic/_internal/_fields.py:151: UserWarning: Field "model_credential_schema" has conflict with protected namespace "model_".

You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`.
  warnings.warn(
/home/vscode/.local/lib/python3.10/site-packages/pydantic/_internal/_fields.py:151: UserWarning: Field "model_type" has conflict with protected namespace "model_".

You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`.
  warnings.warn(
 * Serving Flask app 'app.py'
 * Debug mode: off
INFO:werkzeug:WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:5000
INFO:werkzeug:Press CTRL+C to quit

Suggested Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
  • optional I have made corresponding changes to the documentation
  • optional I have added tests that prove my fix is effective or that my feature works
  • optional New and existing unit tests pass locally with my changes
**Original Pull Request:** https://github.com/langgenius/dify/pull/3017 **State:** closed **Merged:** No --- # Description pydantic 2 introduced multiple enhancement, faster performance, and it was released 9 months ago, it's time to move forward and get better performance, more features. summary of the changes: 1. upgrade langchain, inference-client qdrant-client to the latest, add pydantic-extra-types as it's a new package now 2. apply changes due to pydantic breaking changes, using `bump-pydantic api` 3. fix a few type errors brought by YAML parse, those errors should exist before the upgrade, the strong type check by pydantic 2 exposed them. 4. rename `model_config` field in `AdvancedChatMessageEntity`, `MultiDatasetRouterAgent`, `AgentConfiguration` to `ai_model_config` because `model_config` is reserved by pydantic 2 This is a big update and could be harmful, I advise someone else set up a coverage report like `codecov`, then I can see which part of the code is not covered by tests and add more tests to bring us more confidence. ## Type of Change Please delete options that are not relevant. - [x] Dependency upgrade # How Has This Been Tested? - [x] Start a server that works ``` @LeoQuote ➜ /workspaces/dify/api (upgrade_pydantic) $ FLASK_APP=app.py flask run /home/vscode/.local/lib/python3.10/site-packages/pydantic/_internal/_fields.py:151: UserWarning: Field "model_type" has conflict with protected namespace "model_". You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`. warnings.warn( None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used. /home/vscode/.local/lib/python3.10/site-packages/pydub/utils.py:170: RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning) /home/vscode/.local/lib/python3.10/site-packages/langchain/llms/__init__.py:548: LangChainDeprecationWarning: Importing LLMs from langchain is deprecated. Importing from langchain will no longer be supported as of langchain==0.2.0. Please import from langchain-community instead: `from langchain_community.llms import Tongyi`. To install langchain-community run `pip install -U langchain-community`. warnings.warn( /home/vscode/.local/lib/python3.10/site-packages/pydantic/_internal/_fields.py:151: UserWarning: Field "model_type_instance" has conflict with protected namespace "model_". You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`. warnings.warn( /home/vscode/.local/lib/python3.10/site-packages/pydantic/_internal/_fields.py:151: UserWarning: Field "model_schema" has conflict with protected namespace "model_". You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`. warnings.warn( WARNING:root:Missing subclass of ExternalDataTool in /workspaces/dify/api/core/external_data_tool/api/api.py, Skip. /home/vscode/.local/lib/python3.10/site-packages/pydantic/_internal/_fields.py:186: UserWarning: Field name "raise_error" shadows an attribute in parent "BaseCallbackHandler"; warnings.warn( /home/vscode/.local/lib/python3.10/site-packages/pydantic/_internal/_fields.py:186: UserWarning: Field name "run_inline" shadows an attribute in parent "BaseCallbackHandler"; warnings.warn( /home/vscode/.local/lib/python3.10/site-packages/pydantic/_internal/_fields.py:151: UserWarning: Field "model_configuration" has conflict with protected namespace "model_". You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`. warnings.warn( /home/vscode/.local/lib/python3.10/site-packages/langchain/__init__.py:29: UserWarning: Importing BasePromptTemplate from langchain root module is no longer supported. Please use langchain_core.prompts.BasePromptTemplate instead. warnings.warn( /home/vscode/.local/lib/python3.10/site-packages/langchain/__init__.py:29: UserWarning: Importing PromptTemplate from langchain root module is no longer supported. Please use langchain_core.prompts.PromptTemplate instead. warnings.warn( /home/vscode/.local/lib/python3.10/site-packages/langchain/__init__.py:29: UserWarning: Importing LLMChain from langchain root module is no longer supported. Please use langchain.chains.LLMChain instead. warnings.warn( /home/vscode/.local/lib/python3.10/site-packages/pydantic/_internal/_fields.py:151: UserWarning: Field "model_credential_schema" has conflict with protected namespace "model_". You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`. warnings.warn( /home/vscode/.local/lib/python3.10/site-packages/pydantic/_internal/_fields.py:151: UserWarning: Field "model_type" has conflict with protected namespace "model_". You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`. warnings.warn( * Serving Flask app 'app.py' * Debug mode: off INFO:werkzeug:WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on http://127.0.0.1:5000 INFO:werkzeug:Press CTRL+C to quit ``` # Suggested Checklist: - [x] I have performed a self-review of my own code - [x] I have commented my code, particularly in hard-to-understand areas - [x] My changes generate no new warnings - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods - [ ] `optional` I have made corresponding changes to the documentation - [ ] `optional` I have added tests that prove my fix is effective or that my feature works - [ ] `optional` New and existing unit tests pass locally with my changes
yindo added the pull-request label 2026-02-21 20:22:05 -05:00
yindo closed this issue 2026-02-21 20:22:05 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#23958