[PR #273] [CLOSED] feat(subagents): Add structured response support for subagents #316

Closed
opened 2026-06-05 17:22:38 -04:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/langchain-ai/deepagentsjs/pull/273
Author: @maahir30
Created: 3/4/2026
Status: Closed

Base: mainHead: subagent-structured-output


📝 Commits (4)

📊 Changes

3 files changed (+562 additions, -18 deletions)

View changed files

📝 libs/deepagents/src/middleware/subagent.test.ts (+291 -0)
📝 libs/deepagents/src/middleware/subagents.int.test.ts (+139 -1)
📝 libs/deepagents/src/middleware/subagents.ts (+132 -17)

📄 Description

Adds the ability for subagents to return structured (schema-validated) responses, both statically at definition time and dynamically at invocation time.

What this enables

Static responseFormat — When defining a subagent spec, you can now set a responseFormat (e.g. toolStrategy(zodSchema)) that gets baked into the subagent's agent graph at build time. Every invocation of that subagent will enforce the schema.

Dynamic response_schema — The supervisor can pass a response_schema (a plain JSON Schema object) in its task tool call. When this happens, the middleware builds a fresh agent graph on the fly with that schema as the responseFormat. This gives the supervisor per-invocation control over the subagent's output shape without needing to predefine every possible format.

How it works

  • When a subagent produces a structuredResponse, returnCommandWithStateUpdate now JSON-serializes it into the ToolMessage content sent back to the supervisor, instead of extracting the last message text. This gives the supervisor predictable, parseable data.
  • Dynamic schema creation only applies to spec-based subagents that don't already have a static responseFormat. For compiled subagents or those with a static format, the response_schema parameter is ignored and the pre-built graph is used as-is.

Changes

  • subagents.ts: Added responseFormat to the SubAgent interface, response_schema to the task tool schema, on-the-fly agent creation logic, and structuredResponse prioritization in returnCommandWithStateUpdate
  • subagent.test.ts: Unit tests for structured response serialization, fallback behavior, and dynamic schema bypass paths
  • subagents.int.test.ts: Integration tests for both dynamic response_schema and static responseFormat with a real LLM

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/langchain-ai/deepagentsjs/pull/273 **Author:** [@maahir30](https://github.com/maahir30) **Created:** 3/4/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `subagent-structured-output` --- ### 📝 Commits (4) - [`15d949e`](https://github.com/langchain-ai/deepagentsjs/commit/15d949eb9a9ef6a7f9f792937f53a1ead40c002d) fix subagents - [`8f95fb6`](https://github.com/langchain-ai/deepagentsjs/commit/8f95fb6b83942c7e9ceb54937593d80af13691c3) add per-invoke response format - [`b246fe2`](https://github.com/langchain-ai/deepagentsjs/commit/b246fe2c40ddac43de14266099ccb554774de866) prettier - [`105e542`](https://github.com/langchain-ai/deepagentsjs/commit/105e542cb6c0a72e8a59a2ce48868b1ed9a106ab) system prompt ### 📊 Changes **3 files changed** (+562 additions, -18 deletions) <details> <summary>View changed files</summary> 📝 `libs/deepagents/src/middleware/subagent.test.ts` (+291 -0) 📝 `libs/deepagents/src/middleware/subagents.int.test.ts` (+139 -1) 📝 `libs/deepagents/src/middleware/subagents.ts` (+132 -17) </details> ### 📄 Description Adds the ability for subagents to return structured (schema-validated) responses, both statically at definition time and dynamically at invocation time. ### What this enables **Static `responseFormat`** — When defining a subagent spec, you can now set a `responseFormat` (e.g. `toolStrategy(zodSchema)`) that gets baked into the subagent's agent graph at build time. Every invocation of that subagent will enforce the schema. **Dynamic `response_schema`** — The supervisor can pass a `response_schema` (a plain JSON Schema object) in its `task` tool call. When this happens, the middleware builds a fresh agent graph on the fly with that schema as the `responseFormat`. This gives the supervisor per-invocation control over the subagent's output shape without needing to predefine every possible format. ### How it works - When a subagent produces a `structuredResponse`, `returnCommandWithStateUpdate` now JSON-serializes it into the `ToolMessage` content sent back to the supervisor, instead of extracting the last message text. This gives the supervisor predictable, parseable data. - Dynamic schema creation only applies to spec-based subagents that don't already have a static `responseFormat`. For compiled subagents or those with a static format, the `response_schema` parameter is ignored and the pre-built graph is used as-is. ### Changes - **`subagents.ts`**: Added `responseFormat` to the `SubAgent` interface, `response_schema` to the `task` tool schema, on-the-fly agent creation logic, and `structuredResponse` prioritization in `returnCommandWithStateUpdate` - **`subagent.test.ts`**: Unit tests for structured response serialization, fallback behavior, and dynamic schema bypass paths - **`subagents.int.test.ts`**: Integration tests for both dynamic `response_schema` and static `responseFormat` with a real LLM --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-06-05 17:22:38 -04:00
yindo closed this issue 2026-06-05 17:22:38 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/deepagentsjs#316