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
13 changes: 8 additions & 5 deletions src/llm/bedrock/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ import {
} from '@/llm/stream/smoother';
import { modelDisallowsAssistantPrefill } from '@/llm/anthropic/utils/message_inputs';
import { linkStreamLimitCanonical } from '@/llm/streamLimits';
import { isBedrockOpenAIModel } from './utils/message_inputs';
import {
isBedrockOpenAIModel,
replaysBedrockReasoning,
} from './utils/message_inputs';
import { neutralizeHarmonyToolConfig } from './utils/harmony';
import { insertBedrockToolCachePoint } from './toolCache';

Expand Down Expand Up @@ -271,7 +274,7 @@ export class CustomChatBedrockConverse extends ChatBedrockConverse {
runManager?: CallbackManagerForLLMRun
): Promise<ChatResult> {
if (
isBedrockOpenAIModel(this.model) ||
!replaysBedrockReasoning(this.model) ||
modelDisallowsAssistantPrefill(this.model)
) {
return this.generateWithLocalConverter(messages, options);
Expand All @@ -293,9 +296,9 @@ export class CustomChatBedrockConverse extends ChatBedrockConverse {

/**
* Non-streaming Converse through this package's converter, as the stream already uses.
* The parent's converter replays reasoning and sends harmony control tokens as written,
* both of which OpenAI models on Bedrock fail on, and it cannot strip a trailing
* assistant prefill that Claude 4.6 and later reject.
* The parent's converter replays reasoning, which OpenAI and xAI models on Bedrock reject,
* and sends harmony control tokens as written, which OpenAI models fail on. It also cannot
* strip a trailing assistant prefill that Claude 4.6 and later reject.
*/
private async generateWithLocalConverter(
messages: BaseMessage[],
Expand Down
14 changes: 14 additions & 0 deletions src/llm/bedrock/reasoning-replay.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ describe('Bedrock reasoning replay in streaming requests', () => {
'arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-3-7-sonnet-20250219-v1:0',
replayReasoning: true,
},
{
model: 'us.xai.grok-4.7',
replayReasoning: false,
},
{
model:
'arn:aws:bedrock:us-west-2:123456789012:inference-profile/us.xai.grok-4.7',
replayReasoning: false,
},
{
model: 'xai.grok-4.7',
applicationInferenceProfile: profileArn,
replayReasoning: false,
},
{
model: 'deepseek.v3.2',
replayReasoning: true,
Expand Down
44 changes: 32 additions & 12 deletions src/llm/bedrock/utils/message_inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1141,13 +1141,12 @@ export interface ConvertToConverseMessagesOptions {
}

/**
* Whether the model is an identified OpenAI model on Bedrock (`openai.*`, a geo/global
* inference profile such as `global.openai.*`, or an ARN naming one). Missing model IDs and
* opaque resource ARNs are not identified.
* The lowercased Bedrock model ID, taken from a foundation-model or inference-profile ARN when
* given one. Undefined for missing IDs and opaque resource ARNs.
*/
export function isBedrockOpenAIModel(model?: string): boolean {
function bedrockModelId(model?: string): string | undefined {
if (model == null || model === '') {
return false;
return undefined;
}
let id = model.toLowerCase();
if (id.startsWith('arn:')) {
Expand All @@ -1156,21 +1155,42 @@ export function isBedrockOpenAIModel(model?: string): boolean {
id
)?.[1];
if (modelId == null) {
return false;
return undefined;
}
id = modelId;
}
return /(?:^|\.)openai\./.test(id);
return id;
}

/**
* Whether the model is an identified OpenAI model on Bedrock (`openai.*`, a geo/global
* inference profile such as `global.openai.*`, or an ARN naming one). Missing model IDs and
* opaque resource ARNs are not identified.
*/
export function isBedrockOpenAIModel(model?: string): boolean {
const id = bedrockModelId(model);
return id != null && /(?:^|\.)openai\./.test(id);
}

/**
* Identifies an xAI (Grok) model on Bedrock, including cross-region inference profiles such as
* `us.xai.*` and ARNs naming one.
*/
export function isBedrockXAIModel(model?: string): boolean {
const id = bedrockModelId(model);
return id != null && /(?:^|\.)xai\./.test(id);
}

/**
* OpenAI Bedrock models reject prior `reasoningContent`, including on Claude-to-GPT handoffs.
* Only suppress replay for identified OpenAI targets; other model families retain the existing
* behavior because their Bedrock reasoning-replay requirements have not been verified here.
* OpenAI and xAI Bedrock models reject prior `reasoningContent` ("This model doesn't support the
* reasoningContent.reasoningText.text field for assistant messages"), including their own reasoning
* from the previous step of a tool loop and Claude-to-GPT handoffs. Only suppress replay for
* identified OpenAI and xAI targets; other model families retain the existing behavior because
* their Bedrock reasoning-replay requirements have not been verified here.
* Missing model IDs and opaque resource ARNs also retain the existing behavior.
*/
export function replaysBedrockReasoning(model?: string): boolean {
return !isBedrockOpenAIModel(model);
return !isBedrockOpenAIModel(model) && !isBedrockXAIModel(model);
}

/**
Expand All @@ -1184,7 +1204,7 @@ export function convertToConverseMessages(
converseSystem: BedrockSystemContentBlock[];
} {
const openAITarget = isBedrockOpenAIModel(options.model);
const replayReasoning = !openAITarget;
const replayReasoning = replaysBedrockReasoning(options.model);
const converseSystem = messages
.filter((msg) => msg._getType() === 'system')
.flatMap((msg) => convertSystemMessageToConverseMessage(msg));
Expand Down