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

Type Annotation Of Context Parameter Defaults To Any Instead Of Any | None In Create_agent And AgentMiddleware

Static type checkers treat context as possibly None even when a value is provided, causing false positives. Changing the annotation to Any eliminates these false positives without runtime impact.

mediumConfidence 75%LangchainRetrieversChainsAgents

Origin Analysis

The context parameter in create_agent and AgentMiddleware is annotated as `Any | None = None`. Since the default is None, type checkers include None in the inferred type, even when callers explicitly pass a non-None value. This causes spurious errors when the context is used without None checks.
1. Install langchain and a type checker (mypy or pyright). 2. Write code that calls create_agent with a context argument, then later accesses an attribute on that context object. 3. Run the type checker. It reports 'context is possibly None' or 'Argument has incompatible type "Any | None"; expected ...' despite the value being provided.

Fixing Code Block

# In create_agent signature (langchain/agents/factory.py): # Replace: context: Any | None = None # With: context: Any = None # In AgentMiddleware.__init__ (langchain/agents/middleware.py): # Replace: context: Any | None = None # With: context: Any = None
Removing `| None` from the annotation leaves `Any`, which is a top type that includes None. This signals to type checkers that any operation is allowed on context, removing false positives while preserving the ability to pass None as default.

Edge Case Audit

This change is type-only but technically breaking for users who rely on the `None` union to enforce explicit None checks. It may mask genuine bugs where context is accidentally left as None. Rollback: restore `Any | None` if stricter checking is desired. No runtime behavior changes; concurrency and cross-platform unaffected.

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