Skip to content

Fix structured outputs #1642

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

Conversation

MicrosoftJames
Copy link
Contributor

Currently, CodeAgent will append the closing code block tag to the model output if it's not present, even when self.use_structured_outputs_internally is set to true. This means that the structured outputs outputs (a JSON object) can't then be parsed as JSON. For example, you end up with something like:

'{"observation": " ... ", "code":" ... "}</code>'

which isn't valid JSON.

Fixed by only appending the the closing code block if self.use_structured_outputs_internally is set to false.

@albertvillanova albertvillanova requested a review from Copilot August 1, 2025 16:25
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Fixes a bug where CodeAgent would append closing code block tags to structured output JSON, making it unparseable. The issue occurred when use_structured_outputs_internally was enabled but the agent still added code block delimiters to the JSON response.

  • Conditionally append closing code block tags only when structured outputs are disabled
  • Add test coverage for the structured outputs functionality to prevent regression

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/smolagents/agents.py Wraps code block tag appending logic with structured outputs check
tests/test_agents.py Adds test to verify structured outputs don't get corrupted with code block tags
Comments suppressed due to low confidence (2)

tests/test_agents.py:2133

  • The test only verifies that the tool call arguments match the expected code, but doesn't verify that the JSON parsing works correctly. Consider adding an assertion that validates the JSON structure remains valid (e.g., json.loads(model.generate.return_value.content) succeeds).
        tool_call: ToolCall = next(agent._step_stream(ActionStep(step_number=1, timing="mock_timing", model_output="")))

tests/test_agents.py:2133

  • The test doesn't verify the core bug being fixed - that closing code block tags are not appended to structured outputs. Consider adding an assertion that checks the model output message content remains unchanged (i.e., still valid JSON without appended tags).
        tool_call: ToolCall = next(agent._step_stream(ActionStep(step_number=1, timing="mock_timing", model_output="")))

@MicrosoftJames MicrosoftJames force-pushed the fix-structured-outputs branch from 5776f95 to 67d528b Compare August 1, 2025 16:38
@MicrosoftJames
Copy link
Contributor Author

MicrosoftJames commented Aug 1, 2025

Ran ruff and force-pushed changes to keep clean commit history

Copy link
Collaborator

@aymeric-roucher aymeric-roucher left a comment

Choose a reason for hiding this comment

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

Thank you, great fix!

@aymeric-roucher aymeric-roucher merged commit 464612a into huggingface:main Aug 5, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants