fix(gemini): resolve 400 stream errors and improve client compatibility
- Filter out empty text parts in Gemini requests to avoid 400 errors. - Inject 'assistant' role into the first streaming chunk for better compatibility with clients like opencode. - Fallback to tool_call_id for Gemini function responses when name is missing.
This commit is contained in:
@@ -250,10 +250,18 @@ async fn chat_completions(
|
||||
// Build stream that yields events wrapped in Result
|
||||
let stream = async_stream::stream! {
|
||||
let mut aggregator = Box::pin(aggregating_stream);
|
||||
let mut first_chunk = true;
|
||||
|
||||
while let Some(chunk_result) = aggregator.next().await {
|
||||
match chunk_result {
|
||||
Ok(chunk) => {
|
||||
let role = if first_chunk {
|
||||
first_chunk = false;
|
||||
Some("assistant".to_string())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let response = ChatCompletionStreamResponse {
|
||||
id: stream_id_sse.clone(),
|
||||
object: "chat.completion.chunk".to_string(),
|
||||
@@ -262,7 +270,7 @@ async fn chat_completions(
|
||||
choices: vec![ChatStreamChoice {
|
||||
index: 0,
|
||||
delta: ChatStreamDelta {
|
||||
role: None,
|
||||
role,
|
||||
content: Some(chunk.content),
|
||||
reasoning_content: chunk.reasoning_content,
|
||||
tool_calls: chunk.tool_calls,
|
||||
|
||||
Reference in New Issue
Block a user