-
Notifications
You must be signed in to change notification settings - Fork 67
fix: preserve upstream error headers on non-streaming Responses #262
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
franciscojavierarceo
merged 3 commits into
vllm-project:main
from
ZichenYuan:fix/responses-upstream-error-headers
Sep 9, 2026
+201
−20
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
171 changes: 171 additions & 0 deletions
171
crates/agentic-server/tests/responses_error_headers_test.rs
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 |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| //! Upstream error metadata must survive the non-streaming Responses executor path. | ||
| //! | ||
| //! Regression coverage for <https://github.com/vllm-project/agentic-api/issues/250>: when a | ||
| //! request is routed through the in-process executor (for example `store: true`) and the | ||
| //! upstream returns a non-2xx response, the gateway must preserve the upstream status, body, | ||
| //! and processed metadata headers (`retry-after`, request IDs, rate-limit headers) and must not | ||
| //! relabel a non-JSON error body as `application/json`. | ||
|
|
||
| // `common` is compiled into every test binary; this one never calls `spawn_mock_llm`, | ||
| // so silence the resulting dead-code warning (same as the other tests that skip it). | ||
| #[allow(dead_code)] | ||
| mod common; | ||
|
|
||
| use axum::Router; | ||
| use axum::response::{IntoResponse, Response}; | ||
| use axum::routing::post; | ||
| use http::{HeaderMap, StatusCode}; | ||
| use tokio::net::TcpListener; | ||
|
|
||
| use common::{spawn_gateway, test_config, test_state}; | ||
|
|
||
| const NON_STREAMING_STORED_REQUEST: &str = r#"{"model":"test","input":"hi","store":true,"stream":false}"#; | ||
| const NON_STREAMING_PROXIED_REQUEST: &str = r#"{"model":"test","input":"hi","store":false,"stream":false}"#; | ||
|
|
||
| /// Spawn a mock upstream whose `POST /v1/responses` always answers with the given error. | ||
| async fn spawn_error_upstream( | ||
| status: StatusCode, | ||
| content_type: &'static str, | ||
| body: &'static str, | ||
| extra_headers: HeaderMap, | ||
| ) -> (String, tokio::task::JoinHandle<()>) { | ||
| let app = Router::new().route( | ||
| "/v1/responses", | ||
| post(move || { | ||
| let extra_headers = extra_headers.clone(); | ||
| async move { | ||
| let mut response = Response::builder() | ||
| .status(status) | ||
| .header("content-type", content_type) | ||
| .body(axum::body::Body::from(body)) | ||
| .unwrap(); | ||
| response.headers_mut().extend(extra_headers); | ||
| response.into_response() | ||
| } | ||
| }), | ||
| ); | ||
| let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); | ||
| let addr = listener.local_addr().unwrap(); | ||
| let handle = tokio::spawn(async move { axum::serve(listener, app).await.unwrap() }); | ||
| (format!("http://{addr}"), handle) | ||
| } | ||
|
|
||
| fn rate_limit_headers() -> HeaderMap { | ||
| let mut headers = HeaderMap::new(); | ||
| headers.insert("retry-after", "7".parse().unwrap()); | ||
| headers.insert("x-request-id", "req_example".parse().unwrap()); | ||
| headers.insert("x-ratelimit-remaining-requests", "0".parse().unwrap()); | ||
| headers | ||
| } | ||
|
|
||
| async fn post_responses(gateway_url: &str, body: &'static str) -> reqwest::Response { | ||
| reqwest::Client::new() | ||
| .post(format!("{gateway_url}/v1/responses")) | ||
| .header("content-type", "application/json") | ||
| .body(body) | ||
| .send() | ||
| .await | ||
| .unwrap() | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn executor_path_preserves_upstream_json_error_metadata() { | ||
| let upstream_body = r#"{"error":{"message":"rate limited","type":"rate_limit_error"}}"#; | ||
| let (llm_url, _upstream) = spawn_error_upstream( | ||
| StatusCode::TOO_MANY_REQUESTS, | ||
| "application/json", | ||
| upstream_body, | ||
| rate_limit_headers(), | ||
| ) | ||
| .await; | ||
| let (gateway_url, _gateway) = spawn_gateway(test_state(&test_config(&llm_url))).await; | ||
|
|
||
| let response = post_responses(&gateway_url, NON_STREAMING_STORED_REQUEST).await; | ||
|
|
||
| assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS); | ||
| assert_eq!(response.headers()["retry-after"], "7"); | ||
| assert_eq!(response.headers()["x-request-id"], "req_example"); | ||
| assert_eq!(response.headers()["x-ratelimit-remaining-requests"], "0"); | ||
| assert_eq!(response.headers()["content-type"], "application/json"); | ||
| assert_eq!(response.text().await.unwrap(), upstream_body); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn executor_path_preserves_upstream_plain_text_error_content_type() { | ||
| let upstream_body = "rate limited"; | ||
| let (llm_url, _upstream) = spawn_error_upstream( | ||
| StatusCode::TOO_MANY_REQUESTS, | ||
| "text/plain", | ||
| upstream_body, | ||
| rate_limit_headers(), | ||
| ) | ||
| .await; | ||
| let (gateway_url, _gateway) = spawn_gateway(test_state(&test_config(&llm_url))).await; | ||
|
|
||
| let response = post_responses(&gateway_url, NON_STREAMING_STORED_REQUEST).await; | ||
|
|
||
| assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS); | ||
| assert_eq!(response.headers()["retry-after"], "7"); | ||
| assert_eq!(response.headers()["x-request-id"], "req_example"); | ||
| assert_eq!(response.headers()["content-type"], "text/plain"); | ||
| assert_eq!(response.text().await.unwrap(), upstream_body); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn executor_path_still_filters_connection_nominated_headers() { | ||
| let mut headers = rate_limit_headers(); | ||
| headers.insert("connection", "x-upstream-hop".parse().unwrap()); | ||
| headers.insert("x-upstream-hop", "1".parse().unwrap()); | ||
| let (llm_url, _upstream) = | ||
| spawn_error_upstream(StatusCode::TOO_MANY_REQUESTS, "text/plain", "rate limited", headers).await; | ||
| let (gateway_url, _gateway) = spawn_gateway(test_state(&test_config(&llm_url))).await; | ||
|
|
||
| let response = post_responses(&gateway_url, NON_STREAMING_STORED_REQUEST).await; | ||
|
|
||
| assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS); | ||
| assert_eq!(response.headers()["retry-after"], "7"); | ||
| assert!(response.headers().get("x-upstream-hop").is_none()); | ||
| } | ||
|
|
||
| /// Control: the stateless proxy path already preserved these headers and must keep doing so. | ||
| #[tokio::test] | ||
| async fn proxy_path_preserves_upstream_error_metadata() { | ||
| let upstream_body = "rate limited"; | ||
| let (llm_url, _upstream) = spawn_error_upstream( | ||
| StatusCode::TOO_MANY_REQUESTS, | ||
| "text/plain", | ||
| upstream_body, | ||
| rate_limit_headers(), | ||
| ) | ||
| .await; | ||
| let (gateway_url, _gateway) = spawn_gateway(test_state(&test_config(&llm_url))).await; | ||
|
|
||
| let response = post_responses(&gateway_url, NON_STREAMING_PROXIED_REQUEST).await; | ||
|
|
||
| assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS); | ||
| assert_eq!(response.headers()["retry-after"], "7"); | ||
| assert_eq!(response.headers()["x-request-id"], "req_example"); | ||
| assert_eq!(response.headers()["content-type"], "text/plain"); | ||
| assert_eq!(response.text().await.unwrap(), upstream_body); | ||
| } | ||
|
|
||
| /// Control: gateway-originated errors keep their JSON envelope and content type. | ||
| #[tokio::test] | ||
| async fn malformed_client_json_still_returns_json_error_envelope() { | ||
| let (llm_url, _upstream) = spawn_error_upstream( | ||
| StatusCode::TOO_MANY_REQUESTS, | ||
| "text/plain", | ||
| "unreachable", | ||
| rate_limit_headers(), | ||
| ) | ||
| .await; | ||
| let (gateway_url, _gateway) = spawn_gateway(test_state(&test_config(&llm_url))).await; | ||
|
|
||
| let response = post_responses(&gateway_url, r#"{"model":"test","input":"hi","store":true"#).await; | ||
|
|
||
| assert_eq!(response.status(), StatusCode::BAD_REQUEST); | ||
| assert_eq!(response.headers()["content-type"], "application/json"); | ||
| assert!(response.headers().get("retry-after").is_none()); | ||
| let body: serde_json::Value = response.json().await.unwrap(); | ||
| assert_eq!(body["error"]["type"], "invalid_request_error"); | ||
| } | ||
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.
is it the import of whole
commonmod in this file causing clippy warning withdead_code? if so can just import only the function needed fromcommonlike line 18 of this file.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.
Good question — I checked, and the warning isn't from the import. The
useline already pulls in onlyspawn_gateway,test_config, andtest_state. Removing#[allow(dead_code)]gives:Each integration test file is its own crate, so
mod common;compiles all ofcommon/mod.rsinto this binary, andspawn_mock_llm(which this test doesn't need) becomes dead code. The other tests that don't use it (messages_test,compaction_test,oidc_auth_test,responses_websocket_test) carry the same attribute for the same reason. I kept it and added a short comment explaining why in f61bdef.