Skip to content
Merged
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
7 changes: 7 additions & 0 deletions src/providers/bedrock/chatComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from './complete';
import { BedrockErrorResponse } from './embed';
import {
getBedrockErrorChunk,
transformAdditionalModelRequestFields,
transformAI21AdditionalModelRequestFields,
transformAnthropicAdditionalModelRequestFields,
Expand Down Expand Up @@ -584,6 +585,8 @@ export const BedrockChatCompleteResponseTransform: (
};

export interface BedrockChatCompleteStreamChunk {
// this is error message from bedrock
message?: string;
contentBlockIndex?: number;
delta?: {
text: string;
Expand Down Expand Up @@ -640,6 +643,9 @@ export const BedrockChatCompleteStreamChunkTransform: (
gatewayRequest
) => {
const parsedChunk: BedrockChatCompleteStreamChunk = JSON.parse(responseChunk);
if (parsedChunk.message) {
return getBedrockErrorChunk(fallbackId, gatewayRequest.model || '');
}
Comment on lines +646 to +648
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Code Refactor

Issue: The error message from Bedrock is not being passed to the error chunk function.
Fix: Pass the error message to the getBedrockErrorChunk function.
Impact: Provides more context in the error response for better debugging.

Suggested change
if (parsedChunk.message) {
return getBedrockErrorChunk(fallbackId, gatewayRequest.model || '');
}
if (parsedChunk.message) {
return getBedrockErrorChunk(fallbackId, gatewayRequest.model || '', parsedChunk.message);
}

if (parsedChunk.stopReason) {
streamState.stopReason = parsedChunk.stopReason;
}
Expand Down Expand Up @@ -740,6 +746,7 @@ export const BedrockChatCompleteStreamChunkTransform: (
}),
tool_calls: toolCalls.length > 0 ? toolCalls : undefined,
},
finish_reason: null,
},
],
})}\n\n`;
Expand Down
23 changes: 23 additions & 0 deletions src/providers/bedrock/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Options } from '../../types/requestBody';
import { GatewayError } from '../../errors/GatewayError';
import { BedrockFinetuneRecord, BedrockInferenceProfile } from './types';
import { FinetuneRequest } from '../types';
import { BEDROCK } from '../../globals';

export const generateAWSHeaders = async (
body: Record<string, any> | string | undefined,
Expand Down Expand Up @@ -494,3 +495,25 @@ export const getFoundationModelFromInferenceProfile = async (
return null;
}
};

export const getBedrockErrorChunk = (id: string, model: string) => {
return [
`data: ${JSON.stringify({
id,
object: 'chat.completion.chunk',
created: Math.floor(Date.now() / 1000),
model: model,
provider: BEDROCK,
choices: [
{
index: 0,
delta: {
role: 'assistant',
finish_reason: 'stop',
},
},
],
})}\n\n`,
`data: [DONE]\n\n`,
];
Comment on lines +499 to +518
Copy link
Contributor

Choose a reason for hiding this comment

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

⚡️ Performance Improvement

Issue: The error chunk doesn't include the actual error message from Bedrock, which could be useful for debugging.
Fix: Pass and include the error message in the response.
Impact: Improves error reporting and makes debugging easier.

Suggested change
export const getBedrockErrorChunk = (id: string, model: string) => {
return [
`data: ${JSON.stringify({
id,
object: 'chat.completion.chunk',
created: Math.floor(Date.now() / 1000),
model: model,
provider: BEDROCK,
choices: [
{
index: 0,
delta: {
role: 'assistant',
finish_reason: 'stop',
},
},
],
})}\n\n`,
`data: [DONE]\n\n`,
];
export const getBedrockErrorChunk = (id: string, model: string, errorMessage?: string) => {
return [
`data: ${JSON.stringify({
id,
object: 'chat.completion.chunk',
created: Math.floor(Date.now() / 1000),
model: model,
provider: BEDROCK,
choices: [
{
index: 0,
delta: {
role: 'assistant',
finish_reason: 'stop',
},
error: errorMessage,
},
],
})}\
\
`,
`data: [DONE]\
\
`,
];

};