Skip to content

Commit 80ebad6

Browse files
committed
fix: Override status for errors in anthropic streams
context: anthropic will always return a 200 status code, even when there is actually an error sent in the event stream itself. this would confuse customers as when looking at the logs in the Helicone dashboard, they would expect to see a response since they see a 200 status code, but instead would see some random JSON (the error). to fix this, we are allowing the stream body parsers to override the status code.
1 parent ae1bc5e commit 80ebad6

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

valhalla/jawn/src/lib/handlers/ResponseBodyHandler.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ export class ResponseBodyHandler extends AbstractLogHandler {
7474

7575
try {
7676
const processedResponseBody = await this.processBody(context);
77+
if (processedResponseBody.data?.statusOverride) {
78+
context.message.log.response.status =
79+
processedResponseBody.data.statusOverride;
80+
}
7781
context.processedLog.response.model = getModelFromResponse(
7882
processedResponseBody.data?.processedBody
7983
);

valhalla/jawn/src/lib/shared/bodyProcessors/IBodyProcessor.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ export interface ParseInput {
1111
export type ParseOutput = {
1212
processedBody: any;
1313
usage?: Usage;
14+
// Enables us to override successful status codes if an error
15+
// occurs mid stream. Eg, Anthropic will always return a 200 for streams,
16+
// but may send an error in the `data` event stream, in which case we want
17+
// to override this so that the logs show up as an "error" in our Helicone
18+
// dashboard.
19+
statusOverride?: number;
1420
};
1521

1622
export interface IBodyProcessor {

valhalla/jawn/src/lib/shared/bodyProcessors/anthropicStreamBodyProcessor.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ export class AnthropicStreamBodyProcessor implements IBodyProcessor {
4747

4848
try {
4949
const data = JSON.parse(line.replace("data:", "").trim());
50+
if ("error" in data) {
51+
return ok({
52+
processedBody: data,
53+
statusOverride: 500,
54+
});
55+
}
5056

5157
// Handle input_json_delta for tool_use
5258
if (

0 commit comments

Comments
 (0)