langchain/libs/community/tests/unit_tests
Lei Zhang f203229b51
community: Fix the failure of ChatSparkLLM after upgrading to Pydantic V2 (#27418)
**Description:**

The test_sparkllm.py can reproduce this issue.


https://github.com/langchain-ai/langchain/blob/master/libs/community/tests/integration_tests/chat_models/test_sparkllm.py#L66

```
Testing started at 18:27 ...
Launching pytest with arguments test_sparkllm.py::test_chat_spark_llm --no-header --no-summary -q in /Users/zhanglei/Work/github/langchain/libs/community/tests/integration_tests/chat_models

============================= test session starts ==============================
collecting ... collected 1 item

test_sparkllm.py::test_chat_spark_llm 

============================== 1 failed in 0.45s ===============================
FAILED                             [100%]
tests/integration_tests/chat_models/test_sparkllm.py:65 (test_chat_spark_llm)
def test_chat_spark_llm() -> None:
>       chat = ChatSparkLLM(
            spark_app_id="your spark_app_id",
            spark_api_key="your spark_api_key",
            spark_api_secret="your spark_api_secret",
        )  # type: ignore[call-arg]

test_sparkllm.py:67: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../../../../core/langchain_core/load/serializable.py:111: in __init__
    super().__init__(*args, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

cls = <class 'langchain_community.chat_models.sparkllm.ChatSparkLLM'>
values = {'spark_api_key': 'your spark_api_key', 'spark_api_secret': 'your spark_api_secret', 'spark_api_url': 'wss://spark-api.xf-yun.com/v3.5/chat', 'spark_app_id': 'your spark_app_id', ...}

    @model_validator(mode="before")
    @classmethod
    def validate_environment(cls, values: Dict) -> Any:
        values["spark_app_id"] = get_from_dict_or_env(
            values,
            ["spark_app_id", "app_id"],
            "IFLYTEK_SPARK_APP_ID",
        )
        values["spark_api_key"] = get_from_dict_or_env(
            values,
            ["spark_api_key", "api_key"],
            "IFLYTEK_SPARK_API_KEY",
        )
        values["spark_api_secret"] = get_from_dict_or_env(
            values,
            ["spark_api_secret", "api_secret"],
            "IFLYTEK_SPARK_API_SECRET",
        )
        values["spark_api_url"] = get_from_dict_or_env(
            values,
            "spark_api_url",
            "IFLYTEK_SPARK_API_URL",
            SPARK_API_URL,
        )
        values["spark_llm_domain"] = get_from_dict_or_env(
            values,
            "spark_llm_domain",
            "IFLYTEK_SPARK_LLM_DOMAIN",
            SPARK_LLM_DOMAIN,
        )
    
        # put extra params into model_kwargs
        default_values = {
            name: field.default
            for name, field in get_fields(cls).items()
            if field.default is not None
        }
>       values["model_kwargs"]["temperature"] = default_values.get("temperature")
E       KeyError: 'model_kwargs'

../../../langchain_community/chat_models/sparkllm.py:368: KeyError
``` 

I found that when upgrading to Pydantic v2, @root_validator was changed
to @model_validator. When a class declares multiple
@model_validator(model=before), the execution order in V1 and V2 is
opposite. This is the reason for ChatSparkLLM's failure.

The correct execution order is to execute build_extra first.


https://github.com/langchain-ai/langchain/blob/langchain%3D%3D0.2.16/libs/community/langchain_community/chat_models/sparkllm.py#L302

And then execute validate_environment.


https://github.com/langchain-ai/langchain/blob/langchain%3D%3D0.2.16/libs/community/langchain_community/chat_models/sparkllm.py#L329

The Pydantic community also discusses it, but there hasn't been a
conclusion yet. https://github.com/pydantic/pydantic/discussions/7434

**Issus:** #27416 

**Twitter handle:** coolbeevip

---------

Co-authored-by: vbarda <vadym@langchain.dev>
2024-10-23 21:17:10 -04:00
..
agent_toolkits
agents multiple: pydantic 2 compatibility, v0.3 (#26443) 2024-09-13 14:38:45 -07:00
callbacks core[patch]: Fix "argument of type 'NoneType' is not iterable" error in LangChainTracer (#26576) 2024-09-17 10:29:46 -07:00
chains multiple: pydantic 2 compatibility, v0.3 (#26443) 2024-09-13 14:38:45 -07:00
chat_loaders
chat_message_histories
chat_models community: Fix the failure of ChatSparkLLM after upgrading to Pydantic V2 (#27418) 2024-10-23 21:17:10 -04:00
cross_encoders
data community: Resolve refs recursively when generating openai_fn from OpenAPI spec (#19002) 2024-09-02 13:17:39 -07:00
docstore
document_compressors
document_loaders community[patch]: add web loader tests (#26728) 2024-09-20 18:29:54 -04:00
document_transformers
embeddings multiple: rely on asyncio_mode auto in tests (#27200) 2024-10-15 16:26:38 +00:00
evaluation
examples
graph_vectorstores community: Cassandra Vector Store: modernize implementation (#27253) 2024-10-22 18:11:11 +00:00
graphs
imports
indexes
llms multiple: rely on asyncio_mode auto in tests (#27200) 2024-10-15 16:26:38 +00:00
load multiple: pydantic 2 compatibility, v0.3 (#26443) 2024-09-13 14:38:45 -07:00
query_constructors add neo4j query constructor for self query (#25288) 2024-08-30 14:54:33 +00:00
retrievers multiple: rely on asyncio_mode auto in tests (#27200) 2024-10-15 16:26:38 +00:00
storage infra: add min version testing to pr test flow (#24358) 2024-07-19 22:03:19 +00:00
tools multiple: rely on asyncio_mode auto in tests (#27200) 2024-10-15 16:26:38 +00:00
utilities multiple: rely on asyncio_mode auto in tests (#27200) 2024-10-15 16:26:38 +00:00
utils partners: Use simsimd types (#25299) 2024-08-23 10:41:39 -04:00
vectorstores community: add sqlite-vec vectorstore (#25003) 2024-09-26 17:37:10 +00:00
__init__.py
conftest.py
test_cache.py
test_dependencies.py multiple: pydantic 2 compatibility, v0.3 (#26443) 2024-09-13 14:38:45 -07:00
test_document_transformers.py
test_graph_vectorstores.py core, community: move graph vectorstores to community (#26678) 2024-09-19 11:38:14 -07:00
test_imports.py
test_sql_database_schema.py infra: add min version testing to pr test flow (#24358) 2024-07-19 22:03:19 +00:00
test_sql_database.py infra: add min version testing to pr test flow (#24358) 2024-07-19 22:03:19 +00:00
test_sqlalchemy.py