Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix (provider/bedrock): support budgetTokens #5088

Merged
merged 7 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
8 changes: 8 additions & 0 deletions .changeset/strange-foxes-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@ai-sdk/amazon-bedrock': patch
'ai-core-examples': patch
---

fix(providers/amazon-bedrock) Standardize on budgetTokens in bedrock provider for reasoning config. Add backwards compatability for reasoning_tokens

tweak(ai-core-examples/amazon-bedrock): Make bedrock examples all use budgetTokens
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ const { text, reasoning, reasoningDetails } = await generateText({
prompt: 'How many people will live in the world in 2040?',
providerOptions: {
bedrock: {
reasoningConfig: { type: 'enabled', budgetTokens: 12000 },
reasoningConfig: { type: 'enabled', budgetTokens: 1024 },
},
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function main() {
maxSteps: 5,
providerOptions: {
bedrock: {
reasoning_config: { type: 'enabled', budget_tokens: 2048 },
reasoning_config: { type: 'enabled', budgetTokens: 2048 },
},
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ async function main() {
temperature: 0.5, // should get ignored (warning)
providerOptions: {
bedrock: {
reasoning_config: { type: 'enabled', budget_tokens: 2048 },
reasoning_config: { type: 'enabled', budgetTokens: 2048 },
},
},
maxRetries: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async function main() {
maxRetries: 0,
providerOptions: {
bedrock: {
reasoning_config: { type: 'enabled', budget_tokens: 2048 },
reasoning_config: { type: 'enabled', budgetTokens: 2048 },
},
},
onError: error => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ async function main() {
prompt: 'What is the weather in San Francisco?',
providerOptions: {
bedrock: {
reasoning_config: { type: 'enabled', budget_tokens: 12000 },
reasoning_config: { type: 'enabled', budgetTokens: 1024 },
},
},
maxSteps: 5,
maxRetries: 5,
});

let enteredReasoning = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function main() {
},
providerOptions: {
bedrock: {
reasoning_config: { type: 'enabled', budget_tokens: 1024 },
reasoning_config: { type: 'enabled', budgetTokens: 1024 },
},
},
maxRetries: 0,
Expand Down
23 changes: 21 additions & 2 deletions packages/amazon-bedrock/src/bedrock-chat-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ export class BedrockChatLanguageModel implements LanguageModelV1 {
// Add reasoning config to additionalModelRequestFields
this.settings.additionalModelRequestFields = {
...this.settings.additionalModelRequestFields,
reasoning_config: { ...reasoningConfigOptions.data },
reasoning_config: {
type: reasoningConfigOptions.data?.type,
budget_tokens: thinkingBudget,
},
};
}

Expand Down Expand Up @@ -572,8 +575,24 @@ const BedrockReasoningConfigOptionsSchema = z
.object({
type: z.union([z.literal('enabled'), z.literal('disabled')]),
budget_tokens: z.number().nullish(),
budgetTokens: z.number().nullish(),
})
.nullish();
.nullish()
.transform(data => {
if (!data) return data;

// Normalize the data to ensure we have budget_tokens
if (data.budgetTokens !== undefined && data.budget_tokens === undefined) {
return {
...data,
budget_tokens: data.budgetTokens,
// Remove budgetTokens to avoid duplication
budgetTokens: undefined,
};
}

return data;
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be solved in the thinking budget definition instead of having a transform (line 131)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah let me try that

Copy link
Contributor Author

@Und3rf10w Und3rf10w Mar 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My hesitation here is that if I do:

const BedrockReasoningConfigOptionsSchema = z
  .object({
    type: z.union([z.literal('enabled'), z.literal('disabled')]),
  })
  .and(
    z.union([
      z.object({ budget_tokens: z.number() }),
      z.object({ budgetTokens: z.number() }),
    ])
  )
  .nullish();

Instead of the transform, it will break type checking here:

const thinkingBudget = reasoningConfigOptions.data?.budget_tokens ??
      reasoningConfigOptions.data?.budgetTokens;

I can fix that using in, but doesn't seem as clean?

e.g.

const thinkingBudget = reasoningConfigOptions.data && 
      ('budget_tokens' in reasoningConfigOptions.data 
        ? reasoningConfigOptions.data.budget_tokens 
        : 'budgetTokens' in reasoningConfigOptions.data 
          ? reasoningConfigOptions.data.budgetTokens 
          : undefined);

I pushed that in: 35cf123

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attempt to simplify: #5093


const BedrockStopReasonSchema = z.union([
z.enum(BEDROCK_STOP_REASONS),
Expand Down
Loading