Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/to-model-output-json.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@openrouter/agent': patch
---

`toModelOutput` can now return `{ type: 'json', value }`, and that value is what the model receives on every tool result path (direct round, background grace settle, approval resume, and auto-approved calls persisted while a batch waits for approval). Previously a `json` result was ignored and the raw `execute` result was sent instead. A `toModelOutput` that returns any other type now fails the call instead of sending the raw result.
11 changes: 9 additions & 2 deletions packages/agent/src/lib/model-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3633,7 +3633,9 @@ export class ModelResult<
result: output,
},
});
return Array.isArray(modelOutput) ? modelOutput : output;
// A string here is the serialized `json` mapper value; parse it back so
// `unsentResultsToAPIFormat` serializes it once.
return Array.isArray(modelOutput) ? modelOutput : JSON.parse(modelOutput);
}

private async computeToolOutputForModel(value: {
Expand Down Expand Up @@ -3673,7 +3675,12 @@ export class ModelResult<
if (modelOutputResult.type === 'content') {
return modelOutputResult.value;
}
return JSON.stringify(value.result.result);
if (modelOutputResult.type === 'json') {
return JSON.stringify(modelOutputResult.value) ?? 'null';
}
throw new Error(
`toModelOutput for "${value.toolCall.name}" must return type 'content' or 'json'`,
);
}

/**
Expand Down
14 changes: 10 additions & 4 deletions packages/agent/src/lib/tool-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,11 +404,17 @@ export type ToolOutputContentItem =
/**
* Result of toModelOutput function.
* The 'content' type passes value array directly as tool output.
* The 'json' type sends `JSON.stringify(value)` as tool output.
*/
export type ToModelOutputResult = {
type: 'content';
value: ToolOutputContentItem[];
};
export type ToModelOutputResult =
| {
type: 'content';
value: ToolOutputContentItem[];
}
| {
type: 'json';
value: unknown;
};

/**
* Function to convert tool execution output to model-facing output.
Expand Down
Loading
Loading