Skip to content

fix: honor agent structured_output_fn/output_cls inside AgentWorkflow#22162

Open
anneheartrecord wants to merge 1 commit into
run-llama:mainfrom
anneheartrecord:fix/agent-structured-output-fn-in-workflow
Open

fix: honor agent structured_output_fn/output_cls inside AgentWorkflow#22162
anneheartrecord wants to merge 1 commit into
run-llama:mainfrom
anneheartrecord:fix/agent-structured-output-fn-in-workflow

Conversation

@anneheartrecord

Copy link
Copy Markdown

Description

Fixes #22159

A FunctionAgent configured with its own structured_output_fn (or output_cls) produced structured output when run directly via agent.run(), but the same agent silently ignored that config when executed through an AgentWorkflow.

The cause is in AgentWorkflow.parse_agent_output: on agent completion it only consulted the workflow-level self.structured_output_fn / self.output_cls, never the running agent's. The single-agent path (BaseWorkflowAgent.parse_agent_output) reads the agent's own config, hence the inconsistency.

The fix falls back to the running agent's structured_output_fn / output_cls when the workflow defines none. Workflow-level config still takes precedence, so existing behavior is unchanged when it's set.

Scope note: this targets the normal completion path (the one in the reported repro). The return_direct short-circuit in aggregate_tool_results doesn't generate structured output, but that's already symmetric with the standalone agent path (BaseWorkflowAgent doesn't either), so it's left as-is to avoid introducing a new inconsistency.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • I added new unit tests to cover this change

Added to tests/agent/workflow/test_agent_with_structured_output.py:

  • agent-level output_cls honored via AgentWorkflow
  • agent-level sync structured_output_fn honored via AgentWorkflow
  • agent-level async structured_output_fn honored via AgentWorkflow
  • workflow-level structured_output_fn still overrides the agent's

All structured-output tests pass locally (uv run pytest tests/agent/workflow/test_agent_with_structured_output.py -k "not openai").

Suggested Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added tests that prove my fix is effective

When a FunctionAgent with its own structured_output_fn (or output_cls) runs
inside an AgentWorkflow, the agent's structured output config was ignored:
aggregate_tool_results only consulted the workflow-level structured_output_fn /
output_cls. The agent's callback fired with agent.run() but not workflow.run().

Fall back to the running agent's structured_output_fn / output_cls when the
workflow defines none, keeping workflow-level config as the precedence winner.
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jun 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: FunctionAgent.structured_output_fn is ignored when executed via AgentWorkflow

1 participant