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

RootModel Unions Fail To Deserialize Document/HumanMessage Due To Constructor TypeError

Pydantic RootModel[Document | HumanMessage].model_validate() raises TypeError from Document.__init__ because Document overrides __init__ with required positional page_content. This prevents union deserialization of well-known LangChain schema objects.

highConfidence 92%PydanticData ValidationJSON SchemaAffected V1.4.7

Origin Analysis

Document (and other legacy models like AgentAction, AgentFinish) override __init__ with a required positional parameter. Pydantic's union branch probing passes the incoming dict as keyword arguments to the member constructor. Since Document.__init__ lacks a default for page_content, it raises TypeError, which Pydantic does not catch during union validation, aborting the entire validation instead of trying the next union member.
1. from pydantic import RootModel; from langchain_core.documents import Document; from langchain_core.messages import HumanMessage 2. WellKnown = RootModel[Document | HumanMessage] 3. payload = HumanMessage(content='hello').model_dump() 4. WellKnown.model_validate(payload) 5. Observe TypeError: Document.__init__() missing 1 required positional argument: 'page_content'

Fixing Code Block

Edge Case Audit

This change preserves normal constructor usage (Document('text', {'a': 1})) but may affect code that catches TypeError for missing page_content; such callers will now receive a ValidationError instead. Also, the sentinel approach must be applied to all other models with similar overridden __init__ (AgentAction, AgentFinish) to fully resolve union issues. Rollback: revert to the original __init__ if unexpected constructor behavior occurs; add regression tests for union deserialization and positional construction.

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