fix(provider/bedrock): Fix Reasoning - Amazon Bedrock Anthropic#7073
Merged
dancer merged 3 commits intovercel:mainfrom Jul 7, 2025
Merged
fix(provider/bedrock): Fix Reasoning - Amazon Bedrock Anthropic#7073dancer merged 3 commits intovercel:mainfrom
dancer merged 3 commits intovercel:mainfrom
Conversation
…ield for Bedrock API - Change reasoningConfig field name to "thinking" in additionalModelRequestFields - Filter out reasoningConfig from providerOptions.bedrock to prevent API errors - Add tests to verify reasoningConfig transformation works for both generate and stream - Fixes "Extra inputs are not permitted" error when using reasoning with Bedrock
dancer
approved these changes
Jul 7, 2025
|
Is it possible to get this released in the v4 / v2 Bedrock provider and not just the v5 / v3-beta Bedrock provider? As is, the bug this fixes exists in v2.2.11 (current) of the Bedrock provider. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
The Bedrock provider was sending
reasoningConfigin the request body when reasoning was enabled, but according to the Bedrock API documentation, the correct field name should bethinking. This caused "Extra inputs are not permitted" errors when using reasoning capabilities with Bedrock models. (#6617)Summary
reasoningConfigtothinkinginadditionalModelRequestFieldswhen reasoning is enabledreasoningConfigfrom being accidentally sent at the top level by filtering it out fromproviderOptions.bedrockthinkinginstead ofreasoningConfig)doGenerateanddoStreamto verify the transformation works correctlyVerification
Manual Testing. Verified againsts the following examples, which are failing in
masterbranch.Before Fix
Details
➜ ai-core git:(main) pnpm tsx src/generate-text/amazon-bedrock-reasoning.ts APICallError [AI_APICallError]: The model returned the following errors: reasoningConfig: Extra inputs are not permitted at <anonymous> (/Users/tle/lab/ai/packages/provider-utils/src/response-handler.ts:56:16) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async postToApi (/Users/tle/lab/ai/packages/provider-utils/src/post-to-api.ts:112:28) at async BedrockChatLanguageModel.doGenerate (/Users/tle/lab/ai/packages/amazon-bedrock/src/bedrock-chat-language-model.ts:196:50) at async fn (/Users/tle/lab/ai/packages/ai/core/generate-text/generate-text.ts:353:32) at async <anonymous> (/Users/tle/lab/ai/packages/ai/core/telemetry/record-span.ts:18:22) at async _retryWithExponentialBackoff (/Users/tle/lab/ai/packages/ai/src/util/retry-with-exponential-backoff.ts:36:12) at async fn (/Users/tle/lab/ai/packages/ai/core/generate-text/generate-text.ts:309:34) at async <anonymous> (/Users/tle/lab/ai/packages/ai/core/telemetry/record-span.ts:18:22) at async generateText (/Users/tle/lab/ai/packages/ai/core/generate-text/generate-text.ts:247:12) { cause: undefined, url: 'https://bedrock-runtime.us-east-1.amazonaws.com/model/us.anthropic.claude-3-7-sonnet-20250219-v1%3A0/converse', requestBodyValues: { system: [], messages: [ [Object] ], additionalModelRequestFields: { reasoningConfig: [Object] }, inferenceConfig: { maxOutputTokens: 6144 }, reasoningConfig: { type: 'enabled', budgetTokens: 2048 } }, statusCode: 400, responseHeaders: { connection: 'keep-alive', 'content-length': '102', 'content-type': 'application/json', date: 'Sat, 05 Jul 2025 10:35:36 GMT', 'x-amzn-errortype': 'ValidationException:http://internal.amazon.com/coral/com.amazon.bedrock/', 'x-amzn-requestid': '<OMITTED>' }, responseBody: '{"message":"The model returned the following errors: reasoningConfig: Extra inputs are not permitted"}', isRetryable: false, data: { message: 'The model returned the following errors: reasoningConfig: Extra inputs are not permitted' }, [Symbol(vercel.ai.error)]: true, [Symbol(vercel.ai.error.AI_APICallError)]: true }After Fix
generate-text/amazon-bedrock-reasoning-chatbot.ts
➜ ai-core git:(fix/bedrock/reasoningConfig) pnpm tsx src/generate-text/amazon-bedrock-reasoning-chatbot.ts You: Hello DefaultStepResult { content: [ { type: 'reasoning', text: `The user has simply greeted me with "Hello". There is no specific request or question that would require the use of the available weatherTool function. At this point, I should just respond with a friendly greeting and possibly indicate that I'm here to help.`, providerMetadata: [Object] }, { type: 'text', text: "Hello! How can I help you today? I'm here to assist you with information, answer questions, or help with various tasks. Is there something specific you'd like to know or discuss?" } ], finishReason: 'stop', usage: { inputTokens: 455, outputTokens: 104, totalTokens: 559, cachedInputTokens: 0 }, warnings: [], request: {}, response: { id: 'aitxt-uvy3GgPrVyR7uWaoY6AgHkch', timestamp: 2025-07-05T10:37:19.527Z, modelId: 'us.anthropic.claude-3-7-sonnet-20250219-v1:0', headers: { connection: 'keep-alive', 'content-length': '1051', 'content-type': 'application/json', date: 'Sat, 05 Jul 2025 10:37:19 GMT', 'x-amzn-requestid': 'OMITTED' }, body: undefined, messages: [ [Object] ] }, providerMetadata: { bedrock: { usage: [Object] } } } The user has simply greeted me with "Hello". There is no specific request or question that would require the use of the available weatherTool function. At this point, I should just respond with a friendly greeting and possibly indicate that I'm here to help. Hello! How can I help you today? I'm here to assist you with information, answer questions, or help with various tasks. Is there something specific you'd like to know or discuss?generate-text/amazon-bedrock-reasoning.ts
➜ ai-core git:(fix/bedrock/reasoningConfig) pnpm tsx src/generate-text/amazon-bedrock-reasoning.ts Reasoning: [ { type: 'reasoning', text: 'Let me count the number of "r"s in the word "strawberry".\n' + '\n' + 'The word "strawberry" is spelled:\n' + 's-t-r-a-w-b-e-r-r-y\n' + '\n' + 'Going through each letter:\n' + '- "s": not an "r"\n' + '- "t": not an "r"\n' + '- "r": this is an "r" (first one)\n' + '- "a": not an "r"\n' + '- "w": not an "r"\n' + '- "b": not an "r"\n' + '- "e": not an "r"\n' + '- "r": this is an "r" (second one)\n' + '- "r": this is an "r" (third one)\n' + '- "y": not an "r"\n' + '\n' + 'So there are 3 occurrences of the letter "r" in the word "strawberry".', providerMetadata: { bedrock: [Object] } } ] Text: There are 3 "r"s in the word "strawberry". Warnings: [ { type: 'unsupported-setting', setting: 'temperature', details: 'temperature is not supported when thinking is enabled' } ]stream-text/amazon-bedrock-reasoning-chatbot.ts
➜ ai-core git:(fix/bedrock/reasoningConfig) pnpm tsx src/stream-text/amazon-bedrock-reasoning-chatbot.ts You: Hello Assistant: URL https://bedrock-runtime.us-east-1.amazonaws.com/model/us.anthropic.claude-3-7-sonnet-20250219-v1%3A0/converse-stream Headers { "content-type": "application/json", "authorization": "<OMITTED>", "x-amz-date": "20250705T103848Z", "x-amz-security-token": "<OMITTED>" } Body { "system": [], "messages": [ { "role": "user", "content": [ { "text": "Hello" } ] } ], "additionalModelRequestFields": { "thinking": { "type": "enabled", "budget_tokens": 2048 } }, "inferenceConfig": { "maxOutputTokens": 6144 }, "toolConfig": { "tools": [ { "toolSpec": { "name": "weather", "description": "Get the weather in a location", "inputSchema": { "json": { "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "location": { "description": "The location to get the weather for", "type": "string" } }, "required": [ "location" ], "additionalProperties": false } } } } ], "toolChoice": { "auto": {} } } } > The user has just greeted me with "Hello". This is a general greeting and doesn't contain any specific request that would require using the available tools. The only tool available to me is the "weather" function, which requires a location parameter to provide weather information. Since the user hasn't asked about weather or mentioned any location, I don't need to use any tools at this point. I should simply respond with a greeting and perhaps let them know what I can help with, particularly mentioning that I can provide weather information since that's the capability I have.Hello! How can I help you today? I can provide information such as checking the weather for a specific location. Is there something specific you'd like to know? You:stream-text/amazon-bedrock-reasoning-fullstream.ts
stream-text/amazon-bedrock-reasoning.ts
Tasks
pnpm changesetin the project root)pnpm prettier-fixin the project root)Related Issues
Fixes #6617