Skip to content

Commit e76fc3a

Browse files
[Tiny agents] Add tool call to messages (#3159)
* Add tool call to messages * Fix style * Remove print statements * override role to assistant if unknown --------- Co-authored-by: célina <[email protected]>
1 parent 57eacd2 commit e76fc3a

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/huggingface_hub/inference/_mcp/mcp_client.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ async def process_single_turn_with_tools(
266266
stream=True,
267267
)
268268

269-
message = {"role": "unknown", "content": ""}
269+
message: Dict[str, Any] = {"role": "unknown", "content": ""}
270270
final_tool_calls: Dict[int, ChatCompletionStreamOutputDeltaToolCall] = {}
271271
num_of_chunks = 0
272272

@@ -304,7 +304,26 @@ async def process_single_turn_with_tools(
304304
# Yield each chunk to caller
305305
yield chunk
306306

307-
if message["content"]:
307+
# Add the assistant message with tool calls (if any) to messages
308+
if message["content"] or final_tool_calls:
309+
# if the role is unknown, set it to assistant
310+
if message.get("role") == "unknown":
311+
message["role"] = "assistant"
312+
# Convert final_tool_calls to the format expected by OpenAI
313+
if final_tool_calls:
314+
tool_calls_list: List[Dict[str, Any]] = []
315+
for tc in final_tool_calls.values():
316+
tool_calls_list.append(
317+
{
318+
"id": tc.id,
319+
"type": "function",
320+
"function": {
321+
"name": tc.function.name,
322+
"arguments": tc.function.arguments or "{}",
323+
},
324+
}
325+
)
326+
message["tool_calls"] = tool_calls_list
308327
messages.append(message)
309328

310329
# Process tool calls one by one

0 commit comments

Comments
 (0)