langchain-ai/langchain

Potential validation issue when using `StateLike` in `Annotation` for a tool

Open

#32,067 建立於 2025年7月16日

在 GitHub 查看
 (3 留言) (4 反應) (1 負責人)Python (136,758 star) (22,617 fork)batch import
help wantedinternal

描述

Privileged issue

  • I am a LangChain maintainer, or was asked directly by a LangChain maintainer to create an issue here.

Issue Content

This code snippet triggers a validation issue. Likely issue in how langchain_core is using Pydantic for the run time validation.

    from langgraph.typing import StateLike

    @tool(name, description=description)
    def handoff_to_agent(
        # Annotation is typed as Any instead of StateLike. StateLike 
        # trigger validation issues from Pydantic / langchain_core interaction.
        state: Annotated[StateLike, InjectedState],
        tool_call_id: Annotated[str, InjectedToolCallId],
    ) -> Command:

Stack trace:

    def create_schema_validator(
        schema: CoreSchema,
        schema_type: Any,
        schema_type_module: str,
        schema_type_name: str,
        schema_kind: SchemaKind,
        config: CoreConfig | None = None,
        plugin_settings: dict[str, Any] | None = None,
    ) -> SchemaValidator | PluggableSchemaValidator:
        """Create a `SchemaValidator` or `PluggableSchemaValidator` if plugins are installed.
    
        Returns:
            If plugins are installed then return `PluggableSchemaValidator`, otherwise return `SchemaValidator`.
        """
        from . import SchemaTypePath
        from ._loader import get_plugins
    
        plugins = get_plugins()
        if plugins:
            return PluggableSchemaValidator(
                schema,
                schema_type,
                SchemaTypePath(schema_type_module, schema_type_name),
                schema_kind,
                config,
                plugins,
                plugin_settings or {},
            )
        else:
>           return SchemaValidator(schema, config)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
E           pydantic_core._pydantic_core.SchemaError: Error building "model" validator:
E             SchemaError: Error building "model-fields" validator:
E             SchemaError: Field "state":
E             SchemaError: Error building "union" validator:
E             SchemaError: Error building "is-instance" validator:
E             SchemaError: 'cls' must be valid as the first argument to 'isinstance'

Workaround:

For now implementation will rely on an Any type

貢獻者指南