langchain-ai/langchain

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

Fermée

#32 067 ouverte le 16 juil. 2025

 (3 commentaires) (4 réactions) (1 personne assignée)Python (22 617 forks)batch import

Métriques du dépôt

Stars
 (136 758 étoiles)
Métriques de merge PR
 (Métriques PR en attente)

Description

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

Guide contributeur