Skip to content

Python: [Bug]: AgentResponse.value throws pydantic ValidationError parse errors in multi-message scenarios when using with_request_info() #6366

@Josh-Weston

Description

@Josh-Weston

Description

File: _types.py
Classes: ChatResponse() and AgentResponse()
Methods: text() and value()

When utilizing response_format (such as a Pydantic BaseModel), the framework relies on lazy evaluation to parse the output when the .value property is accessed. However, .value feeds self.text directly to the JSON/Pydantic parser, and self.text blindly concatenates the text of all messages in the response history (return "".join(msg.text for msg in self.messages))

In multi-turn agent loops (e.g., involving intermediate tool calls or reasoning traces), this concatenates conversational text with the final JSON payload, creating an invalid string like:

{ "skill_name": "building-permit-compliance"}{"compliant": True}

The with_request_info Nuance: This architectural flaw is often masked during standard automated runs because the orchestrator simply passes the raw .text to the next agent without ever touching .value. However, if a developer uses .with_request_info() (e.g., for Human-in-the-Loop workflows), the framework is forced to pause, serialize the event state, and access .value. This triggers the lazy evaluation, feeds the concatenated invalid string into the parser, and immediately crashes the workflow with a ValidationError or JSONDecodeError

Providing response_format, skills_provider (or any tool), and with_request_info will cause a PydanticValidation because the multi-turn conversation is concatenated before the call to model_validate_json in the _parse_structured_response_value function

Code Sample

def create_compliance_agent():
    chat_client = OpenAIChatCompletionClient(
        model="not_used",  # required but not used since we set the model in the agent options
        base_url=os.getenv("HF_API_BASE_URL"),
        api_key=os.getenv("HF_API_KEY"),
    )

    skills_provider = SkillsProvider.from_paths(
        skill_paths=pathlib.Path(__file__).parent / "skills"
    )

    return chat_client.as_agent(
        instructions=(
            "You are a building permit compliance agent that determines whether a building permit application is compliant based on the extracted data from the application and the relevant building codes."
            "You will be given the extracted data from a building permit application and you should determine whether the application is compliant with the information in the building permit compliance skill."
            "You MUST use the load_skill tool to read the 'building-permit-compliance' skill before making any decisions."
        ),
        name="building_permit_compliance_agent",
        tools=[],
        context_providers=[skills_provider],
        default_options={
            "model": MODEL,
            "response_format": ComplianceResult,
        },
    )

def create_workflow():
    data_agent = create_data_agent()
    compliance_agent = create_compliance_agent()
    return (
        SequentialBuilder(
            participants=[read_pdf, data_agent,
                          compliance_agent],
            chain_only_agent_responses=True,
        )
        .with_request_info(agents=[compliance_agent])
        .build()
    )

The input received by the pydantic model (notice how the call to the skills provider is sent too because of how self.text is accessed:

Error Messages / Stack Traces

{
  "skill_name": "building-permit-compliance"
}
{
  "compliant": false,
  "reasons": [
    "Contractor \"Pacific Coast Contractors LLC\" is not on the list of approved contractors.",
    "The application pertains to a commercial renovation rather than a solar panel installation, which falls outside the scope of the solar panel compliance criteria."
  ]
}



pydantic_core._pydantic_core.ValidationError: 1 validation error for ComplianceResult
  Invalid JSON: trailing characters at line 5 column 1 [type=json_invalid, input_value='{\n  "skill_name": "buil...ance criteria."\n  ]\n}', input_type=str]
    For further information visit https://errors.pydantic.dev/2.13/v/json_invalid

Package Versions

agent-framework-core: 1.8.0

Python Version

Python 3.13.5

Additional Context

There are ways to manually work around this; however, it would be good to know if the behaviour is expected and by design, or if it is unexpected and will be fixed.

Metadata

Metadata

Assignees

No one assigned

    Type

    No fields configured for Bug.

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions