From 18c19b930420a278b12a85a8dd361a0d1a052abf Mon Sep 17 00:00:00 2001 From: wsp Date: Tue, 3 Nov 2026 21:36:39 +0800 Subject: [PATCH] fix(ai): ignore blank OpenAI finish reasons Treat empty and whitespace-only finish_reason values from OpenAI-compatible gateways as intermediate stream chunks. This prevents premature tool-call finalization from discarding accumulated arguments while preserving documented terminal reasons. Add adapter and end-to-end SSE fixture coverage for blank finish reasons. --- .../ai-adapters/src/stream/types/openai.rs | 47 +++++++++++++++++++ .../stream_processor_openai.rs | 21 +++++++++ .../openai/tool_args_blank_finish_reason.sse | 9 ++++ 3 files changed, 77 insertions(+) create mode 100644 src/crates/adapters/ai-adapters/tests/fixtures/stream/openai/tool_args_blank_finish_reason.sse diff --git a/src/crates/adapters/ai-adapters/src/stream/types/openai.rs b/src/crates/adapters/ai-adapters/src/stream/types/openai.rs index 17320e0a32..d0f27ab33b 100644 --- a/src/crates/adapters/ai-adapters/src/stream/types/openai.rs +++ b/src/crates/adapters/ai-adapters/src/stream/types/openai.rs @@ -204,6 +204,11 @@ impl OpenAISSEData { // can replay structurally present thinking blocks when a provider requires it. let content = content.filter(|s| !s.is_empty()); let reasoning_content = reasoning_content.or(reasoning); + // OpenAI's Chat Completions contract uses `null` until a choice has + // actually finished. Some OpenAI-compatible gateways serialize that + // state as an empty string; treat it as an intermediate chunk so it + // cannot finalize and discard a tool-call argument buffer. + let finish_reason = finish_reason.filter(|reason| !reason.trim().is_empty()); // MiniMax uses `reasoning_details` instead of `reasoning_content`. // Collect all "reasoning.text" entries and join them as a fallback. @@ -336,6 +341,48 @@ mod tests { ); } + #[test] + fn treats_blank_finish_reason_as_an_intermediate_chunk() { + for blank_reason in ["", " "] { + let raw = r#"{ + "id": "chatcmpl_test", + "created": 123, + "model": "gateway-test", + "choices": [{ + "index": 0, + "delta": { + "tool_calls": [{ + "index": 0, + "id": "call_1", + "type": "function", + "function": { + "name": "Read", + "arguments": "{\"path\":\"src/main.rs\"}" + } + }] + }, + "finish_reason": "__BLANK_REASON__" + }] + }"# + .replace("__BLANK_REASON__", blank_reason); + + let sse_data: OpenAISSEData = + serde_json::from_str(&raw).expect("valid openai sse data"); + let responses = sse_data.into_unified_responses(); + + assert_eq!(responses.len(), 1); + assert!(responses[0].finish_reason.is_none()); + assert!(responses[0].tool_call_completion.is_none()); + assert_eq!( + responses[0] + .tool_call + .as_ref() + .and_then(|tool| tool.arguments.as_deref()), + Some(r#"{"path":"src/main.rs"}"#) + ); + } + } + #[test] fn splits_multiple_tool_calls_in_first_choice() { let raw = r#"{ diff --git a/src/crates/adapters/ai-adapters/tests/ai_stream_contracts/stream_processor_openai.rs b/src/crates/adapters/ai-adapters/tests/ai_stream_contracts/stream_processor_openai.rs index 215fc8a2a0..6bca95e31a 100644 --- a/src/crates/adapters/ai-adapters/tests/ai_stream_contracts/stream_processor_openai.rs +++ b/src/crates/adapters/ai-adapters/tests/ai_stream_contracts/stream_processor_openai.rs @@ -70,6 +70,27 @@ async fn openai_fixture_keeps_collecting_tool_args_across_usage_chunks() { ); } +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn openai_fixture_ignores_blank_finish_reason_until_tool_call_completion() { + let output = run_stream_fixture( + StreamFixtureProvider::OpenAi, + "stream/openai/tool_args_blank_finish_reason.sse", + FixtureSseServerOptions::default(), + ) + .await; + + let result = output.result.expect("stream result"); + + assert_eq!(result.tool_calls.len(), 1); + assert_eq!(result.tool_calls[0].tool_id, "call_1"); + assert_eq!(result.tool_calls[0].tool_name, "Read"); + assert_eq!( + result.tool_calls[0].arguments, + json!({ "path": "src/main.rs" }) + ); + assert!(!result.tool_calls[0].is_error); +} + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn openai_fixture_keeps_malformed_tool_arguments_invalid() { let output = run_stream_fixture( diff --git a/src/crates/adapters/ai-adapters/tests/fixtures/stream/openai/tool_args_blank_finish_reason.sse b/src/crates/adapters/ai-adapters/tests/fixtures/stream/openai/tool_args_blank_finish_reason.sse new file mode 100644 index 0000000000..4ae9b1ef61 --- /dev/null +++ b/src/crates/adapters/ai-adapters/tests/fixtures/stream/openai/tool_args_blank_finish_reason.sse @@ -0,0 +1,9 @@ +data: {"id":"chatcmpl_test","object":"chat.completion.chunk","created":123,"model":"gateway-test","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"call_1","type":"function","function":{"name":"Read","arguments":""}}]},"finish_reason":""}]} + +data: {"id":"chatcmpl_test","object":"chat.completion.chunk","created":124,"model":"gateway-test","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"","type":"function","function":{"name":"","arguments":"{\"path\":\""}}]},"finish_reason":""}]} + +data: {"id":"chatcmpl_test","object":"chat.completion.chunk","created":125,"model":"gateway-test","choices":[{"index":0,"delta":{"tool_calls":[{"index":0,"id":"","type":"function","function":{"name":"","arguments":"src/main.rs\"}"}}]},"finish_reason":""}]} + +data: {"id":"chatcmpl_test","object":"chat.completion.chunk","created":126,"model":"gateway-test","choices":[{"index":0,"delta":{},"finish_reason":"tool_calls"}]} + +data: [DONE]