Skip to content
Merged
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
4 changes: 4 additions & 0 deletions nah_chat/CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ CHANGELOG of nah_chat
merging into an existing `reasoning` object (other keys, e.g. `summary`, are preserved).
* Additive only — `insert()` / `reasoning(Value)` behaviour is unchanged; no new public types and
no version bump.
* Fix tool call `id` / `type` duplication in streamed chat completion deltas. Some providers resend
the complete value in every chunk of the same tool call; it was concatenated and grew
(`call_abccall_abc`). `id` / `type` are complete scalars, so they now use last-non-empty-wins
semantics, while `function.arguments` / `function.name` keep accumulating.

## v0.8.0
* Add token usage support to the chat completion stream. `chat_completion_stream` now yields
Expand Down
14 changes: 12 additions & 2 deletions nah_chat/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,22 @@ impl ChatMessage {
});
}
let object_to_apply = message_tool_calls.get_mut(idx).unwrap();
// `id` and `type` are complete, server-assigned scalars, not token
// streams: the latest non-empty value wins. Some providers resend the
// full value in every chunk of the same tool call, so appending them
// would duplicate the value (e.g. "call_abccall_abc"). An empty string
// is treated as "absent" so that a delta carrying `"id": ""` does not
// erase the real id.
tool_call.id.and_then(|id| {
object_to_apply.id.push_str(&id);
if !id.is_empty() {
object_to_apply.id = id;
}
Some(())
});
tool_call._type.and_then(|t| {
object_to_apply._type.push_str(&t);
if !t.is_empty() {
object_to_apply._type = t;
}
Some(())
});
tool_call.function.and_then(|fcall| {
Expand Down
189 changes: 189 additions & 0 deletions nah_chat/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,195 @@ fn test_apply_tool_calls() {
}
}

/**
* Build a chunk delta that carries a single tool call at `index`. `None` leaves
* a field absent from the chunk, like a well-behaved streaming provider does for
* the fields it does not resend.
*/
fn tool_call_delta(
index: usize,
id: Option<&str>,
call_type: Option<&str>,
name: Option<&str>,
arguments: Option<&str>,
) -> ChatResponseChunkDelta {
let function = if name.is_some() || arguments.is_some() {
Some(FunctionCallRequestChunkDelta {
name: name.map(str::to_owned),
arguments: arguments.map(str::to_owned),
})
} else {
None
};
ChatResponseChunkDelta {
role: None,
content: None,
reasoning_content: None,
tool_calls: Some(vec![ToolCallRequestChunkDelta {
index,
id: id.map(str::to_owned),
_type: call_type.map(str::to_owned),
function,
}]),
}
}

#[test]
fn test_apply_tool_call_repeated_id_and_type_are_not_duplicated() {
// Some providers resend the *complete* tool call `id` and `type` in every
// delta chunk of the same tool call. Replacing the value with the same string
// must be idempotent instead of concatenating it again (which would produce
// e.g. "call_abc123call_abc123").
let mut message = ChatMessage::new();
message.apply_model_response_chunk(tool_call_delta(
0,
Some("call_abc123"),
Some("function"),
Some("get_weather"),
Some(""),
));
message.apply_model_response_chunk(tool_call_delta(
0,
Some("call_abc123"),
Some("function"),
None,
Some("{\"city\""),
));
message.apply_model_response_chunk(tool_call_delta(
0,
Some("call_abc123"),
Some("function"),
None,
Some(":\"SF\"}"),
));

let tool_calls = message.tool_calls.as_ref().unwrap();
assert_eq!(tool_calls.len(), 1);
assert_eq!(tool_calls[0].id, "call_abc123");
assert_eq!(tool_calls[0]._type, "function");
assert_eq!(tool_calls[0].function.name, "get_weather");
assert_eq!(tool_calls[0].function.arguments, "{\"city\":\"SF\"}");
}

#[test]
fn test_apply_tool_call_new_id_and_type_replace_the_previous_value() {
// `id` / `type` are complete values, not token streams, so a later non-empty
// value replaces the one accumulated so far (last value wins).
let mut message = ChatMessage::new();
message.apply_model_response_chunk(tool_call_delta(
0,
Some("call_old"),
Some("function"),
Some("get_weather"),
Some(""),
));
message.apply_model_response_chunk(tool_call_delta(
0,
Some("call_new"),
Some("function"),
None,
Some("{}"),
));

let tool_calls = message.tool_calls.as_ref().unwrap();
assert_eq!(tool_calls.len(), 1);
assert_eq!(tool_calls[0].id, "call_new");
assert_eq!(tool_calls[0]._type, "function");
assert_eq!(tool_calls[0].function.name, "get_weather");
assert_eq!(tool_calls[0].function.arguments, "{}");
}

#[test]
fn test_apply_tool_call_empty_id_and_type_do_not_clobber() {
// A provider may send an empty string instead of omitting `id`/`type` on the
// deltas that do not carry them; an empty value must not erase the real one.
let mut message = ChatMessage::new();
message.apply_model_response_chunk(tool_call_delta(
0,
Some("call_abc123"),
Some("function"),
Some("get_weather"),
Some("{\"a"),
));
message.apply_model_response_chunk(tool_call_delta(0, Some(""), Some(""), None, Some(":1}")));

let tool_calls = message.tool_calls.as_ref().unwrap();
assert_eq!(tool_calls.len(), 1);
assert_eq!(tool_calls[0].id, "call_abc123");
assert_eq!(tool_calls[0]._type, "function");
assert_eq!(tool_calls[0].function.name, "get_weather");
assert_eq!(tool_calls[0].function.arguments, "{\"a:1}");
}

#[test]
fn test_apply_tool_call_repeated_id_and_type_per_index() {
// Parallel tool calls: the keep-vs-append decision must be made per index, so
// one tool call resending its id does not affect another one.
let mut message = ChatMessage::new();
message.apply_model_response_chunk(ChatResponseChunkDelta {
role: None,
content: None,
reasoning_content: None,
tool_calls: Some(vec![
ToolCallRequestChunkDelta {
index: 0,
id: Some("call_0".to_owned()),
_type: Some("function".to_owned()),
function: Some(FunctionCallRequestChunkDelta {
name: Some("first".to_owned()),
arguments: Some(String::new()),
}),
},
ToolCallRequestChunkDelta {
index: 1,
id: Some("call_1".to_owned()),
_type: Some("function".to_owned()),
function: Some(FunctionCallRequestChunkDelta {
name: Some("second".to_owned()),
arguments: Some(String::new()),
}),
},
]),
});
// Resend the full id/type for both tool calls, then stream the arguments.
message.apply_model_response_chunk(ChatResponseChunkDelta {
role: None,
content: None,
reasoning_content: None,
tool_calls: Some(vec![
ToolCallRequestChunkDelta {
index: 0,
id: Some("call_0".to_owned()),
_type: Some("function".to_owned()),
function: Some(FunctionCallRequestChunkDelta {
name: None,
arguments: Some("{\"a\":1}".to_owned()),
}),
},
ToolCallRequestChunkDelta {
index: 1,
id: Some("call_1".to_owned()),
_type: Some("function".to_owned()),
function: Some(FunctionCallRequestChunkDelta {
name: None,
arguments: Some("{\"b\":2}".to_owned()),
}),
},
]),
});

let tool_calls = message.tool_calls.as_ref().unwrap();
assert_eq!(tool_calls.len(), 2);
assert_eq!(tool_calls[0].id, "call_0");
assert_eq!(tool_calls[0]._type, "function");
assert_eq!(tool_calls[0].function.name, "first");
assert_eq!(tool_calls[0].function.arguments, "{\"a\":1}");
assert_eq!(tool_calls[1].id, "call_1");
assert_eq!(tool_calls[1]._type, "function");
assert_eq!(tool_calls[1].function.name, "second");
assert_eq!(tool_calls[1].function.arguments, "{\"b\":2}");
}

#[test]
fn test_chat_completion_params_builder() {
let mut params_builder = ChatCompletionParamsBuilder::new();
Expand Down
Loading