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
11 changes: 10 additions & 1 deletion xinference/model/llm/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,16 @@ def test_transform_messages_preserves_tool_call_fields():
assert transformed[1] == {
"role": "assistant",
"content": None,
"tool_calls": messages[1]["tool_calls"],
"tool_calls": [
{
"id": "call_bed4c5f1",
"function": {
"arguments": {"file_path": "README*"},
"name": "view_file_in_detail",
},
"type": "function",
}
],
}
assert transformed[2] == {
"role": "tool",
Expand Down
20 changes: 20 additions & 0 deletions xinference/model/llm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,26 @@ def _transform_messages(
)
new_message = dict(msg)
new_message["content"] = new_content if new_content else None
# Parse JSON-encoded arguments in tool_calls to dicts,
# so Jinja2 templates can iterate them with |items.
if new_message.get("tool_calls"):
tool_calls = []
for tc in new_message["tool_calls"]:
tc = dict(tc)
func = tc.get("function")
if isinstance(func, dict) and isinstance(
func.get("arguments"), str
):
func = dict(func)
try:
parsed_args = json.loads(func["arguments"])
if isinstance(parsed_args, dict):
func["arguments"] = parsed_args
except (json.JSONDecodeError, TypeError):
pass
tc["function"] = func
Comment thread
la1ty marked this conversation as resolved.
tool_calls.append(tc)
new_message["tool_calls"] = tool_calls
Comment on lines +814 to +833
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This change introduces a breaking change for models whose chat templates expect tool_calls arguments to be a JSON string. As noted in the PR description, templates using string concatenation or assuming a string type will fail. Instead of a global conversion in _transform_messages, a safer approach would be to add a fromjson filter to the Jinja2 environment in _compile_jinja_template. This allows templates that need structured data to opt-in without breaking others. If this global change is desired, it should likely be made conditional based on the model family or template requirements.

transformed_messages.append(new_message)

return transformed_messages
Expand Down
Loading