Skip to content

feat(sdk): add static structured output to subagent response#2437

Open
Maahir Sachdev (maahir30) wants to merge 5 commits intomainfrom
structured-output-subagents
Open

feat(sdk): add static structured output to subagent response#2437
Maahir Sachdev (maahir30) wants to merge 5 commits intomainfrom
structured-output-subagents

Conversation

@maahir30
Copy link
Copy Markdown
Contributor

Port of langchain-ai/deepagentsjs#290 to Python.

  • Add response_format field to SubAgent TypedDict, allowing structured output schemas to be passed through to create_agent
  • When a subagent produces a structured_response, JSON-serialize it as the ToolMessage content instead of extracting the last message text
  • Pass response_format through in both the legacy and new SubAgentMiddleware APIs

Integration test ported from langchain-ai/deepagentsjs#423.

@github-actions github-actions bot added deepagents Related to the `deepagents` SDK / agent harness feature New feature/enhancement or request for one internal User is a member of the `langchain-ai` GitHub organization size: M 200-499 LOC labels Apr 3, 2026
skills: NotRequired[list[str]]
"""Skill source paths for SkillsMiddleware."""

response_format: NotRequired[ResponseFormat[Any] | type | dict[str, Any]]
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of documenting example usage, could we document what the options are?

What is type ? (i.e., allows pydantic model, dataclass etc)

waht is dict? (i think it's json schema)

Given that we allow a dataclass, does this code work b/c there's a json.dumps?


structured = result.get("structured_response")
if structured is not None:
content: str = structured.model_dump_json() if hasattr(structured, "model_dump_json") else json.dumps(structured)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the json.dumps work? I think response format may be a dataclass?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missed that part. fixed by adding in a conditional check for data classes and then json dump accordingly

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added tests too to make sure

@github-actions github-actions bot added size: L 500-999 LOC and removed size: M 200-499 LOC labels Apr 3, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Apr 3, 2026

Merging this PR will not alter performance

✅ 32 untouched benchmarks
⏩ 15 skipped benchmarks1


Comparing structured-output-subagents (22b57f2) with main (ebeed42)

Open in CodSpeed

Footnotes

  1. 15 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

and child agents.
"""

import json
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we just use CityPopulation.model_validate_json(...) instead of importing json

Comment on lines +462 to +463
structured = result.get("structured_response")
if structured is not None:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

little nit

Suggested change
structured = result.get("structured_response")
if structured is not None:
if (structured := result.get("structured_response")) is not None:

Comment on lines +400 to +403
create_agent_kwargs: dict[str, Any] = {}
if "response_format" in agent_:
create_agent_kwargs["response_format"] = agent_["response_format"]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eh no need to thread this through the legacy API, we'll remove

@sydney-runkle
Copy link
Copy Markdown
Collaborator

gah sorry, review was pending

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

deepagents Related to the `deepagents` SDK / agent harness feature New feature/enhancement or request for one internal User is a member of the `langchain-ai` GitHub organization size: L 500-999 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants