Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Go] avoid JSON-escaping string tool outputs in tool messages #5310

@acynothia

Description

@acynothia

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:

Chapter1
Chapter2

is currently sent as:
"Chapter1\nChapter2\n"

But it should be sent as:

Chapter1
Chapter2

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(...)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions