Skip to content

Commit

Permalink
chore: Handle overloaded error in anthropic streaming response
Browse files Browse the repository at this point in the history
  • Loading branch information
narengogi committed Nov 11, 2024
1 parent 213273d commit 8b166a9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/providers/anthropic/chatComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ export interface AnthropicChatCompleteStreamResponse {
cache_read_input_tokens?: number;
};
};
error?: AnthropicErrorObject;
}

export const AnthropicErrorResponseTransform: (
Expand Down Expand Up @@ -480,6 +481,10 @@ export const AnthropicChatCompleteStreamChunkTransform: (
chunk = chunk.trim();

const parsedChunk: AnthropicChatCompleteStreamResponse = JSON.parse(chunk);

if (parsedChunk.type === 'error' && parsedChunk.error)
throw new Error('Provider exception: ' + parsedChunk.error.message);

if (
parsedChunk.type === 'content_block_start' &&
parsedChunk.content_block?.type === 'text'
Expand Down
3 changes: 3 additions & 0 deletions src/providers/google-vertex-ai/chatComplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,9 @@ export const VertexAnthropicChatCompleteStreamChunkTransform: (

const parsedChunk: AnthropicChatCompleteStreamResponse = JSON.parse(chunk);

if (parsedChunk.type === 'error' && parsedChunk.error)
throw new Error('Provider exception: ' + parsedChunk.error.message);

if (
parsedChunk.type === 'content_block_start' &&
parsedChunk.content_block?.type === 'text'
Expand Down

0 comments on commit 8b166a9

Please sign in to comment.