-
Notifications
You must be signed in to change notification settings - Fork 458
Feat: structured output #4207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
VascoSch92
merged 23 commits into
OpenHands:main
from
luciobaiocchi:feat/2566-structured-output
Aug 6, 2026
Merged
Feat: structured output #4207
Changes from 9 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
b854499
https://github.com/OpenHands/software-agent-sdk/issues/2566
VascoSch92 2de4af5
address comments
VascoSch92 ef699bd
last small thing
VascoSch92 50316a4
fix(tool): reject reserved meta-field names in response_schema
luciobaiocchi 4c2b491
Merge branch 'main' into feat/2566-structured-output
luciobaiocchi 093af31
Update tool.py
luciobaiocchi d442f11
refactor(tool): use a generator in parse_last_response
luciobaiocchi baf3c28
fix(tool): persist structured output safely
luciobaiocchi 90933d7
Merge upstream main into feat/2566-structured-output
luciobaiocchi d7c3b53
fix(sdk): preserve structured output compatibility
luciobaiocchi a7a662e
docs(sdk): defer structured output example
luciobaiocchi b4a9d09
Merge branch 'main' into feat/2566-structured-output
VascoSch92 a5d6e71
Merge branch 'main' into feat/2566-structured-output
VascoSch92 1f6de46
fix(sdk): reject unsupported response schemas
luciobaiocchi d4b16e4
merge: sync upstream main at v1.39.1
luciobaiocchi 529e292
fix(sdk): preserve structured response contracts
luciobaiocchi 78f9ecd
Merge remote-tracking branch 'origin/main' into feat/2566-structured-…
openhands-agent 7acd49f
Cache normalized response schema to avoid recomputation per tool call
openhands-agent 978b402
fix(sdk): cache response_schema JSON by class, not per-instance Priva…
openhands-agent 216995f
Merge branch 'main' into feat/2566-structured-output
VascoSch92 a5e549d
Merge remote-tracking branch 'refs/remotes/upstream/main' into feat/2…
luciobaiocchi a76b2d6
fix(tool): guard the response-schema JSON cache with its own lock
luciobaiocchi e586b87
Merge branch 'main' into feat/2566-structured-output
VascoSch92 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| """Attach structured output schemas to existing tools.""" | ||
|
|
||
| import os | ||
| from typing import cast | ||
|
|
||
| from pydantic import BaseModel, Field | ||
|
|
||
| from openhands.sdk import LLM, Agent, Conversation | ||
| from openhands.sdk.event import ActionEvent | ||
| from openhands.sdk.tool import Tool, register_tool | ||
| from openhands.sdk.tool.builtins.finish import FinishTool | ||
| from openhands.tools.file_editor import FileEditorTool | ||
| from openhands.tools.terminal import TerminalAction, TerminalTool | ||
|
|
||
|
|
||
| class CommandRationale(BaseModel): | ||
| purpose: str = Field(description="Why this command is being run, in one line.") | ||
| expected_outcome: str = Field( | ||
| description="What the assistant expects to observe from running it." | ||
| ) | ||
|
|
||
|
|
||
| class ProjectFacts(BaseModel): | ||
| description: str = Field(description="One-paragraph description of the project.") | ||
| facts: list[str] = Field(description="Three concise, distinct facts.") | ||
|
|
||
|
|
||
| register_tool("FinishTool", FinishTool) | ||
|
|
||
|
|
||
| llm = LLM( | ||
| model=os.getenv("LLM_MODEL", "anthropic/claude-sonnet-4-5-20250929"), | ||
| api_key=os.getenv("LLM_API_KEY"), | ||
| base_url=os.getenv("LLM_BASE_URL", None), | ||
| ) | ||
|
|
||
| agent = Agent( | ||
| llm=llm, | ||
| tools=[ | ||
| Tool(name=TerminalTool.name, params={"response_schema": CommandRationale}), | ||
| Tool(name=FileEditorTool.name), | ||
| Tool(name="FinishTool", params={"response_schema": ProjectFacts}), | ||
| ], | ||
| include_default_tools=["ThinkTool"], | ||
| ) | ||
|
|
||
| conversation = Conversation(agent=agent, workspace=os.getcwd()) | ||
| conversation.send_message( | ||
| "Inspect the repo using terminal commands, then finish with three facts " | ||
| "about the project." | ||
| ) | ||
| conversation.run() | ||
|
|
||
| events = conversation.state.events | ||
| terminal_tool = agent.tools_map[TerminalTool.name] | ||
| finish_tool = agent.tools_map["finish"] | ||
|
|
||
| print("\n[Terminal commands with rationale]") | ||
|
|
||
| for event in events: | ||
| if ( | ||
| isinstance(event, ActionEvent) | ||
| and event.tool_name == TerminalTool.name | ||
| and event.action is not None | ||
| ): | ||
| assert isinstance(event.action, TerminalAction) | ||
| rationale = cast(CommandRationale, terminal_tool.parse_response(event.action)) | ||
| print(f" $ {event.action.command}") | ||
| print(f" purpose: {rationale.purpose}") | ||
| print(f" expected_outcome: {rationale.expected_outcome}") | ||
|
|
||
| facts = cast(ProjectFacts | None, finish_tool.parse_last_response(events)) | ||
| if facts: | ||
| print("\n[Finish]") | ||
| print(f" description: {facts.description}") | ||
| for fact in facts.facts: | ||
| print(f" - {fact}") | ||
|
|
||
| cost = conversation.conversation_stats.get_combined_metrics().accumulated_cost | ||
| print(f"\nEXAMPLE_COST: {cost}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.