Skip to content

Commit 01c2d5d

Browse files
authored
Merge pull request #1250 from b4s36t4/fix/x-ai-response-transform
fix: transform x-ai error response to open-ai format
2 parents b28924f + 8dfe74c commit 01c2d5d

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

src/providers/x-ai/index.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,43 @@ import {
88
responseTransformers,
99
} from '../open-ai-base';
1010

11+
interface XAIErrorResponse {
12+
error:
13+
| {
14+
message: string;
15+
code: string;
16+
param: string | null;
17+
type: string | null;
18+
}
19+
| string;
20+
code?: string;
21+
}
22+
23+
const xAIResponseTransform = <T>(response: T) => {
24+
let _response = response as XAIErrorResponse;
25+
if ('error' in _response) {
26+
return {
27+
error: {
28+
message: _response.error as string,
29+
code: _response.code ?? null,
30+
param: null,
31+
type: null,
32+
},
33+
provider: X_AI,
34+
};
35+
}
36+
return response;
37+
};
38+
1139
const XAIConfig: ProviderConfigs = {
1240
chatComplete: chatCompleteParams([], { model: 'grok-beta' }),
1341
complete: completeParams([], { model: 'grok-beta' }),
1442
embed: embedParams([], { model: 'v1' }),
1543
api: XAIAPIConfig,
1644
responseTransforms: responseTransformers(X_AI, {
17-
chatComplete: true,
18-
complete: true,
19-
embed: true,
45+
chatComplete: xAIResponseTransform,
46+
complete: xAIResponseTransform,
47+
embed: xAIResponseTransform,
2048
}),
2149
};
2250

0 commit comments

Comments
 (0)