Skip to content

feat (provider/openai): support custom tools#12948

Merged
shaper merged 17 commits intomainfrom
02-27-feat_provider_openai_support_custom_tools
Feb 28, 2026
Merged

feat (provider/openai): support custom tools#12948
shaper merged 17 commits intomainfrom
02-27-feat_provider_openai_support_custom_tools

Conversation

@shaper
Copy link
Copy Markdown
Collaborator

@shaper shaper commented Feb 28, 2026

background

adds support for openai custom tools in responses and fixes alias mapping failures that could cause runtime errors

refs https://developers.openai.com/api/reference/resources/responses/methods/create/

summary

  • add custom tool support for responses with grammar formats
  • resolve aliased custom tool names end to end across tool choice, parsing, and streaming
  • map provider tool names back to sdk tool keys so tool calls return the user-facing key
  • support custom tool output content mapping instead of dropping to empty output
  • add repro examples for forced and unforced alias flows

before this fix

  • forced alias tool choice could fail with api call errors because custom tools were resolved as function tool choice
  • unforced alias calls could fail with no such tool errors when provider name and sdk key differed

after this fix

  • forced alias tool choice resolves to { type: 'custom', name: 'write_sql' } and executes correctly
  • returned tool call and tool result names stay as sdk key alias_name
  • unforced runs no longer fail due to alias mismatch and may validly return no tool calls when model answers directly

repro

repro-alias-forced

before fix (368bbdd468)

git checkout 368bbdd468
cd examples/ai-functions
pnpm tsx src/generate-text/openai/repro-alias-forced.ts

result

  • AI_APICallError: Tool choice 'function' not found in 'tools' parameter
  • request body includes tool_choice: { type: 'function', name: 'alias_name' }

after fix (a3565b08c2)

git checkout a3565b08c2
cd examples/ai-functions
pnpm tsx src/generate-text/openai/repro-alias-forced.ts

result

  • succeeds with tool execution
  • tool call and tool result use sdk key alias_name
  • Steps: 2
repro-alias-unforced

before fix (368bbdd468)

git checkout 368bbdd468
cd examples/ai-functions
pnpm tsx src/generate-text/openai/repro-alias-unforced.ts

observed behavior (multiple runs)

  • run 1: direct text response, toolCalls: []
  • run 2: AI_NoSuchToolError when model emits write_sql but available tool key is alias_name
  • run 3: direct text response, toolCalls: []

after fix (a3565b08c2)

git checkout a3565b08c2
cd examples/ai-functions
pnpm tsx src/generate-text/openai/repro-alias-unforced.ts

observed behavior (multiple runs)

  • no alias mismatch errors
  • direct text responses with toolCalls: [], toolResults: [], Steps: 2

verification

  • pnpm tsx src/generate-text/openai/responses-custom-tool.ts
  • pnpm tsx src/stream-text/openai/responses-custom-tool.ts
  • pnpm tsx src/generate-text/openai/responses-custom-tool-multi-turn.ts
  • pnpm tsx src/stream-text/openai/responses-custom-tool-multi-turn.ts

checklist

  • tests have been added / updated (for bug fixes / features)
  • documentation has been added / updated (for bug fixes / features)
  • a patch changeset for relevant packages has been added (run pnpm changeset in root)
  • i have reviewed this pull request (self-review)

related issues

fixes #12614

Copy link
Copy Markdown
Collaborator Author

shaper commented Feb 28, 2026

This stack of pull requests is managed by Graphite. Learn more about stacking.

@tigent tigent bot added ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label feature New feature or request provider/openai Issues related to the @ai-sdk/openai provider labels Feb 28, 2026
@shaper shaper requested review from R-Taneja and dancer February 28, 2026 04:20
@shaper shaper marked this pull request as ready for review February 28, 2026 04:20
@mclenhard mclenhard self-requested a review February 28, 2026 04:51
@shaper shaper merged commit 58bc42d into main Feb 28, 2026
19 checks passed
@shaper shaper deleted the 02-27-feat_provider_openai_support_custom_tools branch February 28, 2026 07:02
@vercel-ai-sdk
Copy link
Copy Markdown
Contributor

vercel-ai-sdk bot commented Feb 28, 2026

🚀 Published in:

Package Version
ai 6.0.105
@ai-sdk/alibaba 1.0.6
@ai-sdk/amazon-bedrock 4.0.69
@ai-sdk/angular 2.0.106
@ai-sdk/anthropic 3.0.50
@ai-sdk/assemblyai 2.0.21
@ai-sdk/azure 3.0.38
@ai-sdk/baseten 1.0.34
@ai-sdk/black-forest-labs 1.0.21
@ai-sdk/bytedance 1.0.1
@ai-sdk/cerebras 2.0.35
@ai-sdk/cohere 3.0.22
@ai-sdk/deepgram 2.0.21
@ai-sdk/deepinfra 2.0.35
@ai-sdk/deepseek 2.0.21
@ai-sdk/elevenlabs 2.0.21
@ai-sdk/fal 2.0.22
@ai-sdk/fireworks 2.0.36
@ai-sdk/gateway 3.0.59
@ai-sdk/gladia 2.0.21
@ai-sdk/google 3.0.34
@ai-sdk/google-vertex 4.0.68
@ai-sdk/groq 3.0.26
@ai-sdk/huggingface 1.0.33
@ai-sdk/hume 2.0.21
@ai-sdk/klingai 3.0.5
@ai-sdk/langchain 2.0.111
@ai-sdk/llamaindex 2.0.105
@ai-sdk/lmnt 2.0.21
@ai-sdk/luma 2.0.21
@ai-sdk/mcp 1.0.22
@ai-sdk/mistral 3.0.21
@ai-sdk/moonshotai 2.0.6
@ai-sdk/open-responses 1.0.3
@ai-sdk/openai 3.0.37
@ai-sdk/openai-compatible 2.0.31
@ai-sdk/perplexity 3.0.20
@ai-sdk/prodia 1.0.18
@ai-sdk/provider-utils 4.0.16
@ai-sdk/react 3.0.107
@ai-sdk/replicate 2.0.21
@ai-sdk/revai 2.0.21
@ai-sdk/rsc 2.0.105
@ai-sdk/svelte 4.0.105
@ai-sdk/togetherai 2.0.35
@ai-sdk/valibot 2.0.17
@ai-sdk/vercel 2.0.33
@ai-sdk/vue 3.0.105
@ai-sdk/xai 3.0.60

dancer added a commit that referenced this pull request Mar 18, 2026
…3071)

## background

followup to #12948. lars feedback: custom tools required a separate
`name` field in args that duplicated the sdk tool key. every other
provider tool derives its name from the key automatically. the extra
`name` introduced alias mapping complexity and the
`resolveProviderToolName` extension to `createToolNameMapping`

## summary

- remove `name` from `customTool` args schema and type
- use the sdk tool key (`tool.name`) as the name sent to openai
- remove `resolveProviderToolName` from `createToolNameMapping` in
provider-utils
- remove alias mapping tests and repro examples
- update docs and examples

## before

```ts
tools: {
  write_sql: openai.tools.customTool({
    name: 'write_sql', // redundant
    description: '...',
  }),
}
```

## after

```ts
tools: {
  write_sql: openai.tools.customTool({
    description: '...',
  }),
}
```

## verification

- `pnpm tsx src/generate-text/openai/responses-custom-tool.ts`
- `pnpm tsx src/stream-text/openai/responses-custom-tool.ts`
- `pnpm tsx
src/generate-text/openai/responses-custom-tool-multi-turn.ts`
- `pnpm tsx src/stream-text/openai/responses-custom-tool-multi-turn.ts`

## checklist

- [x] tests have been added / updated (for bug fixes / features)
- [x] documentation has been added / updated (for bug fixes / features)
- [x] a _patch_ changeset for relevant packages has been added (run
`pnpm changeset` in root)
- [x] i have reviewed this pull request (self-review)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label feature New feature or request provider/openai Issues related to the @ai-sdk/openai provider

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support OpenAI type: "custom" tools (including grammar) in AI SDK

3 participants