Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/crates/adapters/ai-adapters/src/stream/types/openai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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#"{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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]
Loading