Skip to content
Draft
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
34 changes: 34 additions & 0 deletions src/crates/ai-adapters/src/stream/stream_handler/openai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ fn is_valid_chat_completion_chunk_weak(event_json: &Value) -> bool {
fn extract_sse_api_error_message(event_json: &Value) -> Option<String> {
let error = event_json.get("error")?;
if let Some(message) = error.get("message").and_then(|value| value.as_str()) {
if let Some(code) = error
.get("code")
.and_then(|value| value.as_str())
.map(str::to_string)
.or_else(|| error.get("code").map(|value| value.to_string()))
{
let mut formatted = format!("code={}, message={}", code, message);
if let Some(request_id) = event_json
.get("request_id")
.or_else(|| event_json.get("requestId"))
.and_then(|value| value.as_str())
{
formatted.push_str(&format!(", request_id={}", request_id));
}
return Some(formatted);
}
return Some(message.to_string());
}
if let Some(message) = error.as_str() {
Expand Down Expand Up @@ -274,6 +290,24 @@ mod tests {
);
}

#[test]
fn extracts_api_error_code_message_and_request_id_from_object_shape() {
let event = serde_json::json!({
"error": {
"code": "1305",
"message": "该模型当前访问量过大,请您稍后再试"
},
"request_id": "20260410100425fab0cd73dea74cc3"
});

assert_eq!(
extract_sse_api_error_message(&event).as_deref(),
Some(
"code=1305, message=该模型当前访问量过大,请您稍后再试, request_id=20260410100425fab0cd73dea74cc3"
)
);
}

#[test]
fn extracts_api_error_message_from_string_shape() {
let event = serde_json::json!({
Expand Down
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

需要将provider_error_with_code.sse内容改为

data: {"error":{"code":"1305","message":"provider temporarily overloaded"},"request_id":"req_1305"}


Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
data: {"error":{"code":"1305","message":"provider temporarily overloaded"},"request_id":"req_1305"}
23 changes: 23 additions & 0 deletions src/crates/core/tests/stream_processor_openai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,29 @@ async fn openai_fixture_parses_inline_think_tags_into_reasoning_content() {
assert_eq!(text_chunks, vec!["Final answer."]);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn openai_fixture_preserves_provider_error_code_and_request_id() {
let output = run_stream_fixture(
StreamFixtureProvider::OpenAi,
"stream/openai/provider_error_with_code.sse",
FixtureSseServerOptions::default(),
)
.await;

let error = output
.result
.expect_err("provider error fixture should fail")
.error
.to_string();

assert!(error.contains("SSE API error"));
assert!(error.contains("code=1305"));
assert!(error.contains("message=provider temporarily overloaded"));
assert!(error.contains("request_id=req_1305"));
assert!(!error.contains("missing field"));
assert!(!error.contains("SSE data schema error"));
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn openai_fixture_reattaches_id_only_prelude_to_following_payload_chunk() {
let output = run_stream_fixture(
Expand Down
Loading