langchain-ai/langchain

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

Chiusa

#32.067 aperta il 16 lug 2025

 (3 commenti) (4 reazioni) (1 assegnatario)Python (22.617 fork)batch import
help wantedinternal

Metriche repository

Star
 (136.758 stelle)
Metriche merge PR
 (Merge medio 10g 2h) (288 PR mergiate in 30 g)

Descrizione

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

Guida contributor