Skip to content

Conversation

panbanda
Copy link

@panbanda panbanda commented Sep 4, 2025

Summary

This PR fixes a critical bug where tool response messages have their content and tool_call_id fields swapped in the JSON output when using the OpenAI compatibility layer.

Problem

When Genkit creates tool response messages, the parameters to openai.ToolMessage() were in the wrong order, resulting in:

{
  "role": "tool",
  "content": "tooluse_B8G0zOhpTIKkZKFc_DRtdQ",  // Should be the response data
  "tool_call_id": "{\"conditions\":\"Clear\",...}"  // Should be the tool ID
}

This causes validation errors with services like AWS Bedrock that enforce strict validation on tool_call_id fields (must match pattern [a-zA-Z0-9_-]+ and be ≤64 chars).

Solution

The OpenAI SDK signature for ToolMessage is:

func ToolMessage[T string | []ChatCompletionContentPartTextParam](content T, toolCallID string)

This PR corrects the parameter order from:

openai.ToolMessage(toolCallID, anyToJSONString(p.ToolResponse.Output))  // Wrong

To:

openai.ToolMessage(anyToJSONString(p.ToolResponse.Output), toolCallID)  // Correct

Impact

This fix ensures tool responses are properly formatted according to the OpenAI API specification, making them compatible with strict validation backends like AWS Bedrock.

Testing

  • Verified the parameter order matches OpenAI SDK documentation
  • Tested with tool-calling examples
  • Confirmed JSON output has fields in correct positions

Copy link

google-cla bot commented Sep 4, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

The parameters to openai.ToolMessage were swapped, causing tool responses
to have their content and tool_call_id fields reversed in the JSON output.

This was causing validation errors with AWS Bedrock which expects:
- tool_call_id: to contain the tool reference ID (matching [a-zA-Z0-9_-]+ pattern)
- content: to contain the actual response data

The incorrect order was putting JSON response data in tool_call_id field,
causing Bedrock validation to fail with regex pattern errors.

Fixed by swapping the parameters to match the OpenAI SDK signature:
ToolMessage(content, toolCallID) instead of ToolMessage(toolCallID, content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

1 participant