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%PydanticAffected 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