Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions crates/lingua/src/processing/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ use crate::providers::openai::model_needs_transforms;
use crate::serde_json;
use crate::serde_json::Value;
use crate::universal::{
AssistantContent, AssistantContentPart, Message, TextContentPart, UniversalReasoningDelta,
UniversalRequest, UniversalResponse, UniversalStreamChoice, UniversalStreamChunk,
UniversalStreamDelta, UniversalToolCallDelta, UniversalToolFunctionDelta, UserContent,
UserContentPart,
AssistantContent, AssistantContentPart, Message, TextContentPart, ToolCallArguments,
UniversalReasoningDelta, UniversalRequest, UniversalResponse, UniversalStreamChoice,
UniversalStreamChunk, UniversalStreamDelta, UniversalToolCallDelta, UniversalToolFunctionDelta,
UserContent, UserContentPart,
};
use serde::de::DeserializeOwned;
use thiserror::Error;
Expand Down Expand Up @@ -694,6 +694,8 @@ fn assistant_content_to_stream_delta(content: &AssistantContent) -> UniversalStr
index: Some(tool_call_index),
id: Some(tool_call_id.clone()),
call_type: Some("function".to_string()),
custom_tool_call: matches!(arguments, ToolCallArguments::Custom(_))
.then_some(true),
function: Some(UniversalToolFunctionDelta {
name: Some(tool_name.clone()),
arguments: Some(arguments.to_string()),
Expand Down Expand Up @@ -2071,6 +2073,42 @@ mod tests {
);
}

#[test]
#[cfg(feature = "openai")]
fn test_response_to_stream_chunk_preserves_responses_custom_tool_call() {
let response = UniversalResponse {
id: None,
id_format: None,
model: Some("gpt-5.6-terra".to_string()),
messages: vec![Message::Assistant {
id: None,
content: AssistantContent::Array(vec![AssistantContentPart::ToolCall {
tool_call_id: "call_custom".to_string(),
tool_name: "exec".to_string(),
arguments: ToolCallArguments::Custom("await tools.exec();".to_string()),
status: None,
caller: None,
encrypted_content: None,
provider_options: None,
provider_executed: None,
}]),
}],
usage: None,
finish_reason: None,
};

let chunk = response_to_stream_chunk(response);
let output = crate::providers::openai::responses_adapter::ResponsesAdapter
.stream_from_universal(&chunk)
.unwrap();

assert_eq!(output["type"], json!("response.output_item.added"));
assert_eq!(output["item"]["type"], json!("custom_tool_call"));
assert_eq!(output["item"]["input"], json!(""));
assert_eq!(output["item"]["call_id"], json!("call_custom"));
assert_eq!(output["item"]["name"], json!("exec"));
}

#[test]
#[cfg(feature = "openai")]
fn test_transform_response_passthrough() {
Expand Down
2 changes: 2 additions & 0 deletions crates/lingua/src/providers/anthropic/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,7 @@ impl ProviderAdapter for AnthropicAdapter {
index: Some(0),
id: part.id,
call_type: Some("function".to_string()),
custom_tool_call: None,
function: Some(UniversalToolFunctionDelta {
name: part.name,
arguments: Some(arguments),
Expand Down Expand Up @@ -1109,6 +1110,7 @@ impl ProviderAdapter for AnthropicAdapter {
index: Some(block_index),
id: Some(id.to_string()),
call_type: Some("function".to_string()),
custom_tool_call: None,
function: Some(UniversalToolFunctionDelta {
name: Some(name.to_string()),
arguments: Some(String::new()),
Expand Down
1 change: 1 addition & 0 deletions crates/lingua/src/providers/bedrock/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ impl ProviderAdapter for BedrockAdapter {
index: Some(content_block_start.content_block_index),
id: Some(tool_use.tool_use_id),
call_type: Some("function".to_string()),
custom_tool_call: None,
function: Some(UniversalToolFunctionDelta {
name: Some(tool_use.name),
arguments: Some(String::new()),
Expand Down
1 change: 1 addition & 0 deletions crates/lingua/src/providers/google/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ impl ProviderAdapter for GoogleAdapter {
})
}),
call_type: Some("function".to_string()),
custom_tool_call: None,
function: Some(UniversalToolFunctionDelta {
name: function_call.name.clone(),
arguments: function_call
Expand Down
76 changes: 75 additions & 1 deletion crates/lingua/src/providers/openai/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,10 @@ impl ProviderAdapter for OpenAIAdapter {
choice_map.insert("index".into(), serde_json::json!(c.index));
choice_map.insert(
"delta".into(),
c.delta.clone().unwrap_or(Value::Object(Map::new())),
c.delta
.clone()
.map(chat_stream_delta_from_universal)
.unwrap_or(Value::Object(Map::new())),
);
let finish_reason_val = match &c.finish_reason {
Some(reason) => Value::String(reason.clone()),
Expand Down Expand Up @@ -771,6 +774,22 @@ impl ProviderAdapter for OpenAIAdapter {
}
}

fn chat_stream_delta_from_universal(mut delta: Value) -> Value {
if let Some(tool_calls) = delta
.as_object_mut()
.and_then(|delta| delta.get_mut("tool_calls"))
.and_then(Value::as_array_mut)
{
for tool_call in tool_calls {
if let Some(tool_call) = tool_call.as_object_mut() {
tool_call.remove("custom_tool_call");
}
}
}

delta
}

// =============================================================================
// Helper Functions
// =============================================================================
Expand Down Expand Up @@ -918,6 +937,61 @@ mod tests {
assert_eq!(value["prompt_cache_key"], json!("cache-key-1"));
}

#[test]
fn test_openai_stream_from_universal_omits_custom_tool_call_marker() {
let adapter = OpenAIAdapter;
let chunk = UniversalStreamChunk::new(
None,
None,
vec![UniversalStreamChoice {
index: 0,
delta: Some(json!({
"role": "assistant",
"content": null,
"tool_calls": [{
"index": 0,
"id": "call_1",
"type": "function",
"custom_tool_call": true,
"function": {
"name": "exec",
"arguments": ""
}
}]
})),
finish_reason: None,
}],
None,
None,
);

let value = adapter.stream_from_universal(&chunk).unwrap();

assert_eq!(
value,
json!({
"object": "chat.completion.chunk",
"choices": [{
"index": 0,
"delta": {
"role": "assistant",
"content": null,
"tool_calls": [{
"index": 0,
"id": "call_1",
"type": "function",
"function": {
"name": "exec",
"arguments": ""
}
}]
},
"finish_reason": null
}]
})
);
}

#[test]
fn test_openai_prompt_cache_key_canonical_value_overrides_stale_extra() {
let adapter = OpenAIAdapter;
Expand Down
Loading
Loading