Repository navigation
chore: sync public mirror from internal #1225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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! { | ||
|
|
@@ -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() | ||
|
|
@@ -2884,7 +2894,9 @@ | |
| }); | ||
| } | ||
| } | ||
| let _ = tx.send(StreamEvent::MessageStop { stop_reason: None }); | ||
| let _ = tx.send(StreamEvent::MessageStop { | ||
| stop_reason: Some(stop_reason), | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -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
|
||
| 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; | ||
|
|
@@ -3065,6 +3103,10 @@ | |
| } | ||
| } | ||
| } | ||
| let _ = tx.send(StreamEvent::ProviderError { | ||
| kind: ProviderStreamErrorKind::TransientProtocol, | ||
| message: "Chat stream ended before [DONE]".to_string(), | ||
| }); | ||
| }) | ||
| }; | ||
|
|
||
|
|
@@ -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 [ | ||
|
|
@@ -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", | ||
| ), | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 👍 / 👎.