AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Mypy Type Error In StructuredTool._Injected_args_keys: Any Returned Instead Of Frozenset[Str]

The `_injected_args_keys` property in `langchain_core.tools.structured.StructuredTool` triggers a mypy error because untyped constants cause the return type to be inferred as `Any`, violating the declared `frozenset[str]` return type. A PR has been opened to add explicit annotations and a cast.

mediumConfidence 75%LangChainChat ModelsChains

Origin Analysis

Module-level constants (e.g., `_INJECTED_ARG_KEYS`) are defined without type annotations, causing mypy to infer them as `frozenset[Any]`. When the property returns these constants directly, the return type is `Any`, conflicting with the annotated `frozenset[str]`.
Install langchain-core from main branch. Create a file importing `StructuredTool`. Run `mypy --strict file.py`. Observe error: 'Returning Any from function declared to return "frozenset[str]"'.

Fixing Code Block

from typing import cast, frozenset # Ensure module-level constants are explicitly typed _INJECTED_ARG_KEYS: frozenset[str] = frozenset({ "run_manager", "callbacks", "metadata", }) class StructuredTool(BaseTool): ... @property def _injected_args_keys(self) -> frozenset[str]: return cast(frozenset[str], _INJECTED_ARG_KEYS)
Adding the type annotation to the constant disambiguates the type for mypy, and the cast ensures the return matches the declared type even if mypy still has trouble due to external code.

Edge Case Audit

This is a typing-only change; no runtime behavior is altered. Ensure that the constant is not mutated elsewhere without updating the annotation. Rolling back the PR will reintroduce the mypy error but will not cause runtime failures. If other code relies on the constant's inferred type, the cast may hide type issues; use with caution in strict mypy environments.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state