Skip to content

Ensure MCP loops output the right response format#89

Merged
sgoedecke merged 2 commits intomainfrom
sgoedecke/ensure-mcp-loops-output-desired-response-format
Aug 6, 2025
Merged

Ensure MCP loops output the right response format#89
sgoedecke merged 2 commits intomainfrom
sgoedecke/ensure-mcp-loops-output-desired-response-format

Conversation

@sgoedecke
Copy link
Contributor

In a tool loop, you can't set response_format because the model needs to be able to think in plain English. But you still need the final response to be in the desired format, so we add response_format only on the last iteration.

In a tool loop, you can't set response_format because the model needs to
be able to think in plain English. But you still need the final response
to be in the desired format, so we add response_format only on the last
iteration.
Copilot AI review requested due to automatic review settings August 5, 2025 22:08
@sgoedecke sgoedecke requested a review from a team as a code owner August 5, 2025 22:08
Copy link
Contributor

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

This PR ensures that MCP loops output responses in the correct format by applying response_format only on the final iteration. The change prevents conflicts between tool calls and structured output formatting.

  • Moves response_format application from first iteration to final iteration only
  • Adds logic to trigger an additional loop iteration when no tool calls are made but response formatting is required
  • Ensures tools are excluded when response formatting is applied to avoid API conflicts

Reviewed Changes

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

File Description
src/inference.ts Implements conditional response format application and final message loop logic
tests/inference.test.ts Adds comprehensive test coverage for response format behavior in MCP loops

@sgoedecke sgoedecke merged commit 7923b92 into main Aug 6, 2025
23 checks passed
@sgoedecke sgoedecke deleted the sgoedecke/ensure-mcp-loops-output-desired-response-format branch August 6, 2025 00:41
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.

3 participants