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

Type Error When Passing MessagesState To Create_agent.Invoke Due To Invariant List Type

The high-level create_agent utility accepts a _InputAgentState that types messages as list[AnyMessage | dict[str, Any]]. Because Python's list is invariant, a MessagesState with messages: list[AnyMessage] is not assignable, causing static type checkers to reject valid runtime code.

mediumConfidence 85%LangchainEmbeddingsAgentslangchain-coreAffected V1.2.10

Origin Analysis

In libs/langchain_v1/langchain/agents/middleware/types.py, _InputAgentState defines messages as an invariant list containing a union of AnyMessage and dict[str, Any]. Since list is invariant, a list of only AnyMessage (as in MessagesState) is not recognized as a subtype, even though it is semantically valid.
1. Import create_agent and MessagesState. 2. Create an agent with create_agent(model='openai:gpt-4o-mini', system_prompt='You are a personalized assistant. Just say hi.'). 3. Call agent.invoke(MessagesState(messages=[HumanMessage(content="What's your name?")])). 4. Observe the type error: Argument of type "MessagesState" cannot be assigned to parameter "input" of type "_InputAgentState | Command[Unknown] | None".

Fixing Code Block

from typing import Sequence class _InputAgentState(TypedDict): messages: Sequence[AnyMessage | dict[str, Any]]
Changing the annotation from list to Sequence introduces covariance: a list[AnyMessage] is a valid Sequence[AnyMessage | dict[str, Any]] because Sequence is covariant in its type parameter. This allows passing MessagesState (whose messages field is list[AnyMessage]) while still accepting lists that contain dictionaries. The change is purely in type annotations and has no runtime effect.

Edge Case Audit

This is a type-only change and does not affect runtime behavior. However, if downstream code relies on the concrete list type (e.g., using list-specific methods on the messages attribute), static analysis may now treat it as Sequence, which lacks methods like append. This is unlikely in read-only contexts but if such usage exists, additional casts or protocol adjustments may be needed. No rollback is required as the change is purely additive for type checking.

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