Skip to content

Commit abbc460

Browse files
committed
fixed tool calling bug;
1 parent edf445d commit abbc460

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/strands/models/llamacpp.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,17 @@
1313
import json
1414
import logging
1515
import mimetypes
16-
from typing import Any, AsyncGenerator, Dict, Optional, Type, TypedDict, TypeVar, Union, cast
16+
from typing import (
17+
Any,
18+
AsyncGenerator,
19+
Dict,
20+
Optional,
21+
Type,
22+
TypedDict,
23+
TypeVar,
24+
Union,
25+
cast,
26+
)
1727

1828
import httpx
1929
from pydantic import BaseModel
@@ -385,7 +395,10 @@ def _format_messages(self, messages: Messages, system_prompt: Optional[str] = No
385395
]
386396
formatted_tool_messages = [
387397
self._format_tool_message(
388-
{"toolUseId": content["toolResult"]["toolUseId"], "content": content["toolResult"]["content"]}
398+
{
399+
"toolUseId": content["toolResult"]["toolUseId"],
400+
"content": content["toolResult"]["content"],
401+
}
389402
)
390403
for content in contents
391404
if "toolResult" in content
@@ -605,6 +618,7 @@ async def stream(
605618

606619
tool_calls: Dict[int, list] = {}
607620
usage_data = None
621+
finish_reason = None
608622

609623
async for line in response.aiter_lines():
610624
if not line.strip() or not line.startswith("data: "):
@@ -650,6 +664,7 @@ async def stream(
650664

651665
# Check for finish reason
652666
if choice.get("finish_reason"):
667+
finish_reason = choice.get("finish_reason")
653668
break
654669

655670
yield self._format_chunk({"chunk_type": "content_stop"})
@@ -702,7 +717,12 @@ async def stream(
702717
yield self._format_chunk({"chunk_type": "content_stop"})
703718

704719
# Send stop reason
705-
stop_reason = "tool_use" if tool_calls else getattr(choice, "finish_reason", "end_turn")
720+
logger.debug("finish_reason=%s, tool_calls=%s", finish_reason, bool(tool_calls))
721+
if finish_reason == "tool_calls" or tool_calls:
722+
stop_reason = "tool_calls" # Changed from "tool_use" to match format_chunk expectations
723+
else:
724+
stop_reason = finish_reason or "end_turn"
725+
logger.debug("stop_reason=%s", stop_reason)
706726
yield self._format_chunk({"chunk_type": "message_stop", "data": stop_reason})
707727

708728
# Send usage metadata if available

0 commit comments

Comments
 (0)