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
6 changes: 3 additions & 3 deletions .repository-projection.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"projection": "deixic-code",
"projectionSchemaVersion": 1,
"sourceRepository": "dx-corp/mono",
"sourceSha": "ff30c9c3a53069771b2d8a5f29b7427ec7652ba5",
"sourceSha": "883bd85c592bbfc7ac95992291f8b02e87011ecd",
"destinationRepository": "dx-corp/code",
"priorProjectedBase": "a3c9d10ec4707bfc14f6ffbeadc8d5bf5e62fcbe",
"priorProjectedBase": "6ede82720df7af1028a4791aeb49593e5c9c7bbc",
"definitionDigest": "82936441c776e3e8edb5d215a75007ec9714a233f489d460075d79d5ef5ba32f",
"toolDigest": "45502ff0478e541d02f34cc39ac332935f8d3c0030fa221afe5bcc3b5e51f88e",
"contentDigest": "ea195e7286ba9cd9fdf9c9b3d5487d6b7ff9f500d76103fcccafbf407003bb0b",
"contentDigest": "cd333a61496a0fd449c9b897ac7dec59e0302939c30d209e47898db02ae480d9",
"publicationEligible": true
}
74 changes: 71 additions & 3 deletions packages/ai-rs/src/openai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@
use super::transform::{OutboundTarget, transform_messages_for_target};
use super::types::{
ContentBlock, ImageSource, ManagedGatewayReceipt, Message, MessageContent,
ProviderStreamErrorKind, RequestConfig, Role, StreamEvent, Tool, ToolSchemaEnforcement,
ProviderStreamErrorKind, RequestConfig, Role, StopReason, StreamEvent, Tool,
ToolSchemaEnforcement,
};

// The managed gateway owns a 45-second default provider attempt, plus
Expand Down Expand Up @@ -2826,6 +2827,7 @@
let mut content_started = false;
let mut thinking_started = false;
let mut content_block_stopped = false;
let mut terminal_reason: Option<StopReason> = None;

loop {
let chunk = tokio::select! {
Expand All @@ -2849,6 +2851,14 @@
}

if line == "data: [DONE]" {
let Some(stop_reason) = terminal_reason else {
let _ = tx.send(StreamEvent::ProviderError {
kind: ProviderStreamErrorKind::TransientProtocol,
message: "Chat stream ended without a finish reason"
.to_string(),
});
return;
};
if !content_block_stopped
&& (content_started || thinking_started)
&& current_tool_calls.is_empty()
Expand Down Expand Up @@ -2884,7 +2894,9 @@
});
}
}
let _ = tx.send(StreamEvent::MessageStop { stop_reason: None });
let _ = tx.send(StreamEvent::MessageStop {
stop_reason: Some(stop_reason),
});
return;
}

Expand All @@ -2899,6 +2911,32 @@
}

for choice in &chunk.choices {
if let Some(reason) = choice.finish_reason.as_deref() {
let reason = match reason {
"stop" => StopReason::EndTurn,
"tool_calls" | "function_call" => {
StopReason::ToolUse
}
"length" => StopReason::MaxTokens,
_ => {
let _ = tx.send(StreamEvent::ProviderError {
kind: ProviderStreamErrorKind::TransientProtocol,
message: "Chat stream returned an unknown finish reason".to_string(),
});

Check failure on line 2925 in packages/ai-rs/src/openai.rs

View workflow job for this annotation

GitHub Actions / unresolved-review-threads / unresolved-review-threads

unresolved P1 review thread

Check failure on line 2925 in packages/ai-rs/src/openai.rs

View workflow job for this annotation

GitHub Actions / unresolved-review-threads / unresolved-review-threads

unresolved P1 review thread

Comment on lines +2921 to +2925

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Handle the valid content_filter finish reason

When OpenAI or Azure OpenAI terminates a chat completion with the documented finish_reason: "content_filter", this wildcard treats the valid terminal response as a transient protocol failure. If no content was emitted, the unified stream layer retries the same filtered request up to its retry limit; otherwise the turn fails after partial output, and usage in the terminal chunk is discarded. Map this reason to an appropriate terminal/non-retryable outcome instead of rejecting it as unknown. Apply the correction in the internal source before mirroring it here.

AGENTS.md reference: AGENTS.md:L7-L9

Useful? React with 👍 / 👎.

return;
}
};
if terminal_reason
.is_some_and(|existing| existing != reason)
{
let _ = tx.send(StreamEvent::ProviderError {
kind: ProviderStreamErrorKind::TransientProtocol,
message: "Chat stream returned conflicting finish reasons".to_string(),
});
return;
}
terminal_reason = Some(reason);
}
if let Some(thinking) = choice.delta.thinking_text() {
if !thinking_started {
thinking_started = true;
Expand Down Expand Up @@ -3065,6 +3103,10 @@
}
}
}
let _ = tx.send(StreamEvent::ProviderError {
kind: ProviderStreamErrorKind::TransientProtocol,
message: "Chat stream ended before [DONE]".to_string(),
});
})
};

Expand Down Expand Up @@ -4081,6 +4123,32 @@

"#;

#[tokio::test]
async fn managed_chat_requires_finish_reason_and_done() {
let delta = "data: {\"id\":\"chat-1\",\"object\":\"chat.completion.chunk\",\"created\":1,\"model\":\"gpt-5.6\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"partial\"},\"finish_reason\":null}]}\n\n";
for sse in [
"".to_string(),
delta.to_string(),
"data: [DONE]\n\n".to_string(),
format!("{delta}data: [DONE]\n\n"),
"data: {\"choices\":[],\"usage\":{\"prompt_tokens\":2,\"completion_tokens\":1}}\n\ndata: [DONE]\n\n".to_string(),
] {
let (mut client, _request_rx) =
managed_gateway_test_client(&sse, &managed_receipt_headers());
let mut authorization: serde_json::Value =
serde_json::from_str(&managed_authorization_fixture("lineage-receipt")).unwrap();
authorization["claims"]["endpoint"] = "chat.completions".into();
client.set_managed_inference_authorization(Some(authorization.to_string()));
let stream = client
.stream(&[], &RequestConfig { model: "gpt-5.6".into(), ..Default::default() })
.await
.unwrap();
let events = collect_stream_events(stream).await;
assert!(events.iter().any(|event| matches!(event, StreamEvent::ProviderError { kind: ProviderStreamErrorKind::TransientProtocol, .. })), "{events:?}");
assert!(!events.iter().any(|event| matches!(event, StreamEvent::MessageStop { .. })), "{events:?}");
}
}

#[tokio::test]
async fn managed_endpoint_selects_url_body_and_stream_parser_over_catalog() {
for (endpoint, model, sse, path, field) in [
Expand All @@ -4094,7 +4162,7 @@
(
"chat.completions",
"gpt-5.6",
"data: {\"id\":\"chat-1\",\"object\":\"chat.completion.chunk\",\"created\":1,\"model\":\"gpt-5.6\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"ok\"},\"finish_reason\":null}]}\n\ndata: [DONE]\n\n",
"data: {\"id\":\"chat-1\",\"object\":\"chat.completion.chunk\",\"created\":1,\"model\":\"gpt-5.6\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"ok\"},\"finish_reason\":null}]}\n\ndata: {\"id\":\"chat-1\",\"object\":\"chat.completion.chunk\",\"created\":1,\"model\":\"gpt-5.6\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}]}\n\ndata: [DONE]\n\n",
"/v1/chat/completions",
"messages",
),
Expand Down
Loading