Skip to content

feat(tools): add toModelOutput for custom tool result formatting#24

Merged
mattapperson merged 1 commit intomainfrom
feature/to-model-output
Apr 14, 2026
Merged

feat(tools): add toModelOutput for custom tool result formatting#24
mattapperson merged 1 commit intomainfrom
feature/to-model-output

Conversation

@mattapperson
Copy link
Copy Markdown
Collaborator

Summary

  • Adds optional toModelOutput function to tool definitions that converts execution output to model-facing content blocks (Responses API format)
  • This decouples what calling code receives (structured data like { imageUrl }) from what the model sees (text + image content blocks)
  • If toModelOutput throws, the error is surfaced (no fallback to JSON.stringify)

Example Usage

const imageGenTool = tool({
  name: 'image_gen',
  inputSchema: z.object({ prompt: z.string() }),
  execute: async ({ prompt }) => {
    const imageUrl = await generateImage(prompt);
    return { status: 'ok', imageUrl };
  },
  toModelOutput: ({ output }) => ({
    type: 'content',
    value: [
      { type: 'input_text', text: 'Image generated successfully.' },
      { type: 'input_image', detail: 'auto', imageUrl: output.imageUrl },
    ],
  }),
});

Test plan

  • All existing tests pass (223 tests)
  • Added 5 new unit tests for toModelOutput:
    • Tool creation with toModelOutput
    • Both output and input params received correctly
    • Async toModelOutput support
    • Tools without outputSchema
    • Generator tools with toModelOutput
  • TypeScript type checking passes

Adds optional toModelOutput function to tool definitions that converts
execution output to model-facing content blocks (Responses API format).
This decouples what calling code receives from what the model sees.
@mattapperson mattapperson merged commit 358253d into main Apr 14, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant