In [generate.go:90](
|
case ai.RoleTool: |
|
for _, p := range msg.Content { |
|
if !p.IsToolResponse() { |
|
continue |
|
} |
|
// Use the captured tool call ID (Ref) if available, otherwise fall back to tool name |
|
toolCallID := p.ToolResponse.Ref |
|
if toolCallID == "" { |
|
toolCallID = p.ToolResponse.Name |
|
} |
|
|
|
toolOutput, err := anyToJSONString(p.ToolResponse.Output) |
|
if err != nil { |
|
g.err = err |
|
return g |
|
} |
|
tm := openai.ToolMessage(toolOutput, toolCallID) |
|
oaiMessages = append(oaiMessages, tm) |
|
} |
|
case ai.RoleUser: |
),
tool response output is currently passed through anyToJSONString(p.ToolResponse.Output) before being sent as an OpenAI tool message.
This causes an issue when p.ToolResponse.Output is already a string. The string gets JSON-encoded again, which adds extra escaping and surrounding quotes. As a result, plain text tool output is turned into a JSON string literal instead of being forwarded as raw text.
For example, this input:
is currently sent as:
"Chapter1\nChapter2\n"
But it should be sent as:
Expected behavior
If p.ToolResponse.Output is already a string, use it directly as the tool message content instead of marshaling it with JSON.
Only non-string values should go through JSON serialization.
Why this matters
This preserves human-readable tool output and avoids unnecessary escaping in downstream model interactions. It also matches the expected behavior for plain text tool results.
if p.ToolResponse.Output is a string, use it directly
otherwise, fall back to anyToJSONString(...)
In [generate.go:90](
genkit/go/plugins/compat_oai/generate.go
Lines 87 to 106 in 6f48b37
tool response output is currently passed through anyToJSONString(p.ToolResponse.Output) before being sent as an OpenAI tool message.
This causes an issue when p.ToolResponse.Output is already a string. The string gets JSON-encoded again, which adds extra escaping and surrounding quotes. As a result, plain text tool output is turned into a JSON string literal instead of being forwarded as raw text.
For example, this input:
is currently sent as:
"Chapter1\nChapter2\n"
But it should be sent as:
Expected behavior
If p.ToolResponse.Output is already a string, use it directly as the tool message content instead of marshaling it with JSON.
Only non-string values should go through JSON serialization.
Why this matters
This preserves human-readable tool output and avoids unnecessary escaping in downstream model interactions. It also matches the expected behavior for plain text tool results.
if p.ToolResponse.Output is a string, use it directly
otherwise, fall back to anyToJSONString(...)