-
-
Notifications
You must be signed in to change notification settings - Fork 830
Claude Optimize: Use structured outputs to guarantee valid JSON from Claude, +3 more #1283
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
Open
saharmor
wants to merge
1
commit into
trycua:main
Choose a base branch
from
saharmor:claude-optimize/00715f26-575e-4b96-87f1-26e365833235
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -800,17 +800,46 @@ async def _run() -> int: | |
| "1. Write a 1-2 sentence summary of what is currently on screen.\n" | ||
| "2. List every interactive element visible (buttons, links, inputs, " | ||
| "menus, checkboxes, dropdowns, etc.) with its center coordinates " | ||
| "in image pixels (origin = top-left). Be precise.\n\n" | ||
| "Respond in this exact JSON format:\n" | ||
| '{"summary": "...", "elements": [{"name": "...", "type": "...", "x": N, "y": N}, ...]}\n' | ||
| "in image pixels (origin = top-left). Be precise." | ||
| ) | ||
| if extra: | ||
| prompt += f"\nAdditional instructions: {extra}" | ||
|
|
||
| screenshot_analysis_tool = { | ||
| "name": "screenshot_analysis", | ||
| "description": "Report the analysis of the screenshot, including a summary and all interactive elements.", | ||
| "input_schema": { | ||
| "type": "object", | ||
| "properties": { | ||
| "summary": { | ||
| "type": "string", | ||
| "description": "1-2 sentence summary of what is currently on screen", | ||
| }, | ||
| "elements": { | ||
| "type": "array", | ||
| "items": { | ||
| "type": "object", | ||
| "properties": { | ||
| "name": {"type": "string", "description": "Element label or text"}, | ||
| "type": {"type": "string", "description": "Element type (button, link, input, etc.)"}, | ||
| "x": {"type": "integer", "description": "Center X coordinate in pixels"}, | ||
| "y": {"type": "integer", "description": "Center Y coordinate in pixels"}, | ||
| }, | ||
| "required": ["name", "type", "x", "y"], | ||
| }, | ||
| "description": "All interactive elements visible on screen", | ||
| }, | ||
| }, | ||
| "required": ["summary", "elements"], | ||
| }, | ||
| } | ||
|
|
||
| img_b64 = base64.b64encode(img_bytes).decode() | ||
| response = client.messages.create( | ||
| model="claude-haiku-4-5-20251001", | ||
| max_tokens=1024, | ||
| tools=[screenshot_analysis_tool], | ||
| tool_choice={"type": "tool", "name": "screenshot_analysis"}, | ||
| messages=[ | ||
| { | ||
| "role": "user", | ||
|
|
@@ -829,26 +858,21 @@ async def _run() -> int: | |
| ], | ||
| ) | ||
|
|
||
| raw = response.content[0].text.strip() | ||
| # Try to parse JSON; fall back to raw text | ||
| try: | ||
| parsed = json.loads(raw) | ||
| summary = parsed.get("summary", "") | ||
| elements = parsed.get("elements", []) | ||
| print(f"✅ snapshot — {save_path}") | ||
| print() | ||
| print(summary) | ||
| if elements: | ||
| print() | ||
| print("Interactive elements:") | ||
| for el in elements: | ||
| print( | ||
| f" • {el.get('name', '?')} [{el.get('type', '?')}] ({el.get('x', '?')}, {el.get('y', '?')})" | ||
| ) | ||
| except json.JSONDecodeError: | ||
| print(f"✅ snapshot — {save_path}") | ||
| # Extract the tool use result — guaranteed valid JSON matching the schema | ||
| tool_block = next(b for b in response.content if b.type == "tool_use") | ||
| parsed = tool_block.input | ||
| summary = parsed.get("summary", "") | ||
| elements = parsed.get("elements", []) | ||
|
Comment on lines
+861
to
+865
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle missing tool_use block gracefully. Using 🛡️ Proposed fix # Extract the tool use result — guaranteed valid JSON matching the schema
- tool_block = next(b for b in response.content if b.type == "tool_use")
+ tool_block = next((b for b in response.content if b.type == "tool_use"), None)
+ if tool_block is None:
+ await _print_context(state["provider"], state.get("name", ""), state)
+ return _fail("AI response missing tool_use block")
parsed = tool_block.input🤖 Prompt for AI Agents |
||
| print(f"✅ snapshot — {save_path}") | ||
| print() | ||
| print(summary) | ||
| if elements: | ||
| print() | ||
| print(raw) | ||
| print("Interactive elements:") | ||
| for el in elements: | ||
| print( | ||
| f" • {el.get('name', '?')} [{el.get('type', '?')}] ({el.get('x', '?')}, {el.get('y', '?')})" | ||
| ) | ||
|
|
||
| except ImportError: | ||
| await _print_context(state["provider"], state.get("name", ""), state) | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Model label mismatch causes silent fallback to the wrong Anthropic model.
At Lines 118-121, the old keys were removed, but
libs/python/agent/agent/ui/gradio/ui_components.py(Lines 41-52) still emits the old labels. Becauseget_model_string()falls back at Line 150, user selection can be ignored without warning.Proposed backward-compatible fix
"anthropic": { "default": "anthropic/claude-sonnet-4-6", + # Backward-compatible labels still used by ui_components.py + "Anthropic: Claude 4 Opus (20250514)": "anthropic/claude-opus-4-6", + "Anthropic: Claude 4 Sonnet (20250514)": "anthropic/claude-sonnet-4-6", + "Anthropic: Claude 3.7 Sonnet (20250219)": "anthropic/claude-sonnet-4-5-20250929", "Anthropic: Claude Opus 4.6": "anthropic/claude-opus-4-6", "Anthropic: Claude Sonnet 4.6": "anthropic/claude-sonnet-4-6", "Anthropic: Claude Sonnet 4.5": "anthropic/claude-sonnet-4-5-20250929", },📝 Committable suggestion