fixed serialization issues with enums on response

This commit is contained in:
Salman Paracha 2025-08-24 13:12:15 -07:00
parent 2fa983d50d
commit 0b41496c45
2 changed files with 14 additions and 1 deletions

View file

@ -104,6 +104,19 @@ impl Iterator for ProviderStreamResponseIter {
}
}
}
// Helper to serialize only the inner struct, not the enum wrapper.
// This avoids the problem where serde serializes the enum variant as a wrapper object in JSON.
impl ProviderResponseType {
/// Serialize the response as JSON bytes, omitting the enum wrapper.
pub fn as_json_bytes(&self) -> Result<Vec<u8>, serde_json::Error> {
match self {
ProviderResponseType::ChatCompletionsResponse(resp) => serde_json::to_vec(resp),
ProviderResponseType::MessagesResponse(resp) => serde_json::to_vec(resp),
}
}
}
pub trait ProviderResponse: Send + Sync {
/// Get usage information if available - returns dynamic trait object
fn usage(&self) -> Option<&dyn TokenUsage>;

View file

@ -681,7 +681,7 @@ impl HttpContext for StreamContext {
match (supported_api, self.resolved_api.as_ref()) {
(Some(supported_api), Some(_)) => {
match ProviderResponseType::try_from((&body[..], supported_api, &provider_id)) {
Ok(response) => match serde_json::to_vec(&response) {
Ok(response) => match response.as_json_bytes() {
Ok(bytes) => {
self.set_http_response_body(0, bytes.len(), &bytes);
}