fix(anthropic): non-stream /v1/messages returned OpenAI shape; strip EOS sentinel from text deltas
BUG: Anthropic client (sourceFormat=CLAUDE) hitting a claude-target provider got a raw OpenAI chat.completion body on non-streaming requests. The needsTranslation(CLAUDE,CLAUDE) gate is false when target===source, so the translator never ran; a claude-transport executor replying OpenAI JSON (opencode/big-pickle) leaked choices[]/prompt_tokens to the client, which Anthropic SDKs cannot parse (no content[] blocks, no type:"message"). FIX: shape-aware guard toClaudeMessageShape() in nonStreamingHandler — when sourceFormat is CLAUDE, convert any OpenAI-shape body to a proper Claude message (type, content blocks with thinking/text/tool_use, stop_reason via finish mapping, usage input/output tokens). Claude-shaped bodies pass through. Also: strip <|im_end|>/<|endoftext|>/<|eot_id|> EOS sentinels from Claude text deltas in openai-to-claude and kiro-to-claude translators — the upstream EOS token leaks into the final text_delta (observed 'OK<|im_end|>'). Tests: anthropic-nonstream-shape.test.js (6: eos strip + shape guard incl tool_calls→tool_use, pass-through, finish mapping). 28/28 related tests green.
This commit is contained in:
1 parent
e0eef05b6b
commit
9fd0fb0181
5 files changed
+137
-3
No files matched your search
@@ -23,6 +23,20 @@ function parseToolArguments(value) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an OpenAI Chat Completions body into the Claude Messages shape when
|
||||
* the client speaks Anthropic (sourceFormat=CLAUDE) but the provider replied
|
||||
* OpenAI JSON. This is the shape-aware guard for the non-streaming path: the
|
||||
* needsTranslation(CLAUDE,CLAUDE) gate is false when target===source, so the
|
||||
* raw provider body would otherwise leak to the Anthropic client with no
|
||||
* content[] blocks and no type:"message".
|
||||
*/
|
||||
export function toClaudeMessageShape(responseBody) {
|
||||
if (!responseBody || responseBody.type === "message") return responseBody;
|
||||
if (responseBody?.choices) return openAICompletionToClaudeMessage(responseBody);
|
||||
return responseBody;
|
||||
}
|
||||
|
||||
function openAICompletionToClaudeMessage(responseBody) {
|
||||
if (!responseBody?.choices?.[0]) return responseBody;
|
||||
const choice = responseBody.choices[0];
|
||||
@@ -346,9 +360,19 @@ export async function handleNonStreamingResponse({ providerResponse, provider, m
|
||||
saveUsageStats({ provider, model, tokens: usage, connectionId, apiKey, endpoint: clientRawRequest?.endpoint, silent: true });
|
||||
if (log?.line) log.line(reqTag, "📊", formatDoneLine({ usage, latency: { total: Date.now() - requestStartTime } }));
|
||||
|
||||
const translatedResponse = needsTranslation(targetFormat, sourceFormat)
|
||||
let translatedResponse = needsTranslation(targetFormat, sourceFormat)
|
||||
? translateNonStreamingResponse(responseBody, targetFormat, sourceFormat, customToolNames)
|
||||
: responseBody;
|
||||
// Shape-aware guard: an Anthropic client (sourceFormat=CLAUDE) must receive a
|
||||
// Claude message even when the provider answered in OpenAI Chat shape. The
|
||||
// needsTranslation(CLAUDE,CLAUDE) gate above is false when target===source
|
||||
// (both CLAUDE), so the raw provider body is returned — and a claude-transport
|
||||
// executor replying OpenAI JSON leaks a chat.completion body to the client
|
||||
// (observed with opencode/big-pickle: no content[], no type:"message").
|
||||
if (sourceFormat === FORMATS.CLAUDE) {
|
||||
translatedResponse = toClaudeMessageShape(translatedResponse);
|
||||
}
|
||||
|
||||
const isClaudeMessageResponse = sourceFormat === FORMATS.CLAUDE && translatedResponse?.type === "message";
|
||||
// Responses-format translation produces a `object:"response"` body with no
|
||||
// `choices`; skip the Chat-Completions-specific post-processing below for it.
|
||||
|
||||
Reference in new issue
Block a user