Skip to content

Deserializing an invalid component raises TypeError: ValueError: 'error' required in context instead of the validation message #269

Open

Description

@fede-kamel

Description

When a component fails a custom validator while being deserialized with AgentSpecDeserializer.from_dict / from_json / from_yaml (or Component.from_dict and friends), the deserializer does not report the validation error. It crashes with TypeError: ValueError: 'error' required in context while rebuilding the ValidationError, and the actual message (which node is unreachable, which input is not in the prompt, which client configuration is not allowed, ...) is lost.

This affects every error raised by a @model_validator_with_error_accumulation validator, i.e. the value_error entries, which is how most Agent Spec components report configuration problems.

Reproduction

from pyagentspec.flows.edges import ControlFlowEdge
from pyagentspec.flows.flow import Flow
from pyagentspec.flows.nodes import EndNode, StartNode
from pyagentspec.serialization import AgentSpecDeserializer

start, end = StartNode(id="start", name="start"), EndNode(id="end", name="end")
flow = Flow(
    id="flow", name="flow", start_node=start, nodes=[start, end],
    control_flow_connections=[ControlFlowEdge(id="edge", name="edge", from_node=start, to_node=end)],
)
config = flow.to_dict()
# add a second end node that no control flow edge reaches: rejected by a Flow validator
unreachable = {**config["$referenced_components"]["end"], "id": "unreachable_end", "name": "unreachable_end"}
config["nodes"].append(unreachable)

AgentSpecDeserializer().from_dict(config)
  File ".../pyagentspec/serialization/pydanticdeserializationplugin.py", line 66, in deserialize
    raise ValidationError.from_exception_data(
        title=component.__class__.__name__,
        line_errors=line_errors,
    )
TypeError: ValueError: 'error' required in context

Building the same invalid Flow directly in Python reports the problem correctly:

pydantic_core._pydantic_core.ValidationError: 1 validation error for Flow
  Value error, Found an end node without any incoming control flow edge, which is not permitted (node is 'unreachable_end'). Please check the control flow edges.

Expected behavior

Deserialization raises a ValidationError carrying the original error type and message, as direct construction does.

Root cause

PydanticComponentDeserializationPlugin.deserialize (pyagentspec/src/pyagentspec/serialization/pydanticdeserializationplugin.py) collects the validation errors of the component and rebuilds a ValidationError with InitErrorDetails(type=e.type, loc=e.loc, input=()). pydantic cannot render error types such as value_error from the type alone: they need the ctx of the original error (the error key), which is not passed, so ValidationError.from_exception_data raises the TypeError. The partial-construction path (from_partial_dict) is not affected because it returns the collected errors instead of rebuilding them.

Environment

  • pyagentspec main (26.4.0.dev0, 5d1fa9b), pydantic 2.12.5, Python 3.13

Activity

  1. added a commit that references this issue on Oct 1, 2026
    3e73dd2
  2. added a commit that references this issue on Oct 9, 2026
    6107693
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions