[router][grpc] Fix inconsistent behavior of conversation_id not found (#12299)

This commit is contained in:
Chang Su
2025-10-28 16:24:43 -07:00
committed by GitHub
parent 29195aaa6e
commit b48354c537
4 changed files with 279 additions and 30 deletions
@@ -48,6 +48,7 @@ use tokio::sync::{mpsc, RwLock};
use tokio_stream::wrappers::UnboundedReceiverStream;
use tracing::{debug, error, warn};
use uuid::Uuid;
use validator::Validate;
use super::{
conversions,
@@ -109,7 +110,32 @@ pub async fn route_responses(
}
}
// 1. Validate mutually exclusive parameters
// 1. Validate request (includes conversation ID format)
if let Err(validation_errors) = request.validate() {
// Extract the first error message for conversation field
let error_message = validation_errors
.field_errors()
.get("conversation")
.and_then(|errors| errors.first())
.and_then(|error| error.message.as_ref())
.map(|msg| msg.to_string())
.unwrap_or_else(|| "Invalid request parameters".to_string());
return (
StatusCode::BAD_REQUEST,
axum::Json(json!({
"error": {
"message": error_message,
"type": "invalid_request_error",
"param": "conversation",
"code": "invalid_value"
}
})),
)
.into_response();
}
// 2. Validate mutually exclusive parameters
if request.previous_response_id.is_some() && request.conversation.is_some() {
return (
StatusCode::BAD_REQUEST,
@@ -125,7 +151,7 @@ pub async fn route_responses(
.into_response();
}
// 2. Check for incompatible parameter combinations
// 3. Check for incompatible parameter combinations
let is_streaming = request.stream.unwrap_or(false);
let is_background = request.background.unwrap_or(false);
@@ -144,7 +170,7 @@ pub async fn route_responses(
.into_response();
}
// 3. Route based on execution mode
// 4. Route based on execution mode
if is_streaming {
route_responses_streaming(ctx, request, headers, model_id).await
} else if is_background {
@@ -928,33 +954,23 @@ async fn load_conversation_history(
if let Some(ref conv_id_str) = request.conversation {
let conv_id = ConversationId::from(conv_id_str.as_str());
// Auto-create conversation if it doesn't exist (OpenAI behavior)
if let Ok(None) = ctx.conversation_storage.get_conversation(&conv_id).await {
debug!(
"Creating new conversation with user-provided ID: {}",
// Check if conversation exists - return error if not found
let conversation = ctx
.conversation_storage
.get_conversation(&conv_id)
.await
.map_err(|e| {
crate::routers::grpc::utils::internal_error_message(format!(
"Failed to check conversation: {}",
e
))
})?;
if conversation.is_none() {
return Err(crate::routers::grpc::utils::bad_request_error(format!(
"Conversation '{}' not found. Please create the conversation first using the conversations API.",
conv_id_str
);
// Convert HashMap to JsonMap for metadata
let metadata = request.metadata.as_ref().map(|m| {
m.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect::<serde_json::Map<String, serde_json::Value>>()
});
let new_conv = crate::data_connector::NewConversation {
id: Some(conv_id.clone()), // Use user-provided conversation ID
metadata,
};
ctx.conversation_storage
.create_conversation(new_conv)
.await
.map_err(|e| {
crate::routers::grpc::utils::internal_error_message(format!(
"Failed to create conversation: {}",
e
))
})?;
)));
}
// Load conversation history