Repository navigation
Pass string tool output to the model without JSON.stringify #909
MatasVS
started this conversation in
Feature Request
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Summary
Pass a
stringtool output to the model as is. KeepJSON.stringifyfor objects, arrays, numbers, booleans andnull.Background & Motivation
Today custom tool output always goes through
JSON.stringifybefore it becomes the tool-result text (packages/runtime/src/session.ts,createCustomToolsandreexecuteDurableToolCall). The docs say this (guide/tools.md,reference/agent-api.md), so it is not a bug. But for a string it gives a bad result.A tool that returns
'a\nb'reaches the model as"a\nb": quotes around it, and the newline is the two characters\n. A tool that returns command output (many lines, quotes, backslashes) gets one long escaped line. This costs more tokens and the model reads it worse than the raw text. The docs examplerun: async () => 'Intake complete.'reaches the model as"Intake complete.".Two more points:
createCustomToolsfunction, the prepared tool adapter branch already passes its text through without change. So two tool kinds have two different text contracts.useTool({ durable: true })) can only use the stringify path, so it has no way to give plain text to the model.I checked what reads this text. The structured value is stored separately (
details.outputand theoutputfield of thetool_outcomerecord), and the public projections use it first.toolResultOutput(content)reads the text only when no structured output exists. I found no place that parses the text back as JSON. So the change is text only, if both sites change together.Goals
details.output,tool_outcome.output) does not change.Example
Model sees today:
Model sees after the change:
Change at both sites in
session.ts:The two docs lines that say the output "is JSON-stringified for the model" need the same update. If a tool needs the old quoted text, it can return
JSON.stringify(value)fromrun.All reactions