language_models: Use message field from Cloud error responses, if present (#38286)

This PR updates the Cloud language model provider to use the `message`
field from the Cloud error response, if it is present.

Previously we would always show the entire JSON payload in the error
message, but with this change we can show just the user-facing `message`
the error response is in a shape that we recognize.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers
2025-09-16 21:45:25 +00:00
committed by GitHub
parent ee912366a3
commit 26202e5af2
+29 -22
View File
@@ -541,29 +541,36 @@ where
impl From<ApiError> for LanguageModelCompletionError {
fn from(error: ApiError) -> Self {
if let Ok(cloud_error) = serde_json::from_str::<CloudApiError>(&error.body)
&& cloud_error.code.starts_with("upstream_http_")
{
let status = if let Some(status) = cloud_error.upstream_status {
status
} else if cloud_error.code.ends_with("_error") {
error.status
} else {
// If there's a status code in the code string (e.g. "upstream_http_429")
// then use that; otherwise, see if the JSON contains a status code.
cloud_error
.code
.strip_prefix("upstream_http_")
.and_then(|code_str| code_str.parse::<u16>().ok())
.and_then(|code| StatusCode::from_u16(code).ok())
.unwrap_or(error.status)
};
if let Ok(cloud_error) = serde_json::from_str::<CloudApiError>(&error.body) {
if cloud_error.code.starts_with("upstream_http_") {
let status = if let Some(status) = cloud_error.upstream_status {
status
} else if cloud_error.code.ends_with("_error") {
error.status
} else {
// If there's a status code in the code string (e.g. "upstream_http_429")
// then use that; otherwise, see if the JSON contains a status code.
cloud_error
.code
.strip_prefix("upstream_http_")
.and_then(|code_str| code_str.parse::<u16>().ok())
.and_then(|code| StatusCode::from_u16(code).ok())
.unwrap_or(error.status)
};
return LanguageModelCompletionError::UpstreamProviderError {
message: cloud_error.message,
status,
retry_after: cloud_error.retry_after.map(Duration::from_secs_f64),
};
return LanguageModelCompletionError::UpstreamProviderError {
message: cloud_error.message,
status,
retry_after: cloud_error.retry_after.map(Duration::from_secs_f64),
};
}
return LanguageModelCompletionError::from_http_status(
PROVIDER_NAME,
error.status,
cloud_error.message,
None,
);
}
let retry_after = None;