-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Description
Describe the bug
I should probably split these out, lmk if that would be helpful.
There are a couple issues with Anthropic tool calling. AFAICT both issues are in
python/semantic_kernel/connectors/ai/anthropic/services/anthropic_chat_completion.py
if an agent self-invokes tools more than 5 times (DEFAULT_MAX_AUTO_INVOKE_ATTEMPTS) the tool list will be removed from the last chat message (assuming to force stop auto invokes?)
This may work for some APIs but it causes Anthropic's API to error out:
Requests which include tool_use or tool_result blocks must define tools.
If it sees tool history, it expects tool calls .. at least that's my interpretation.
_get_tool_calls_from_message does not always yield hashable FunctionCallContents. It passes a dict for the arguments param, which is not hashable. The FCCs seem to be serialized between group chat handoffs (?) which forces the error. My workaround has been to serialize the tool call arguments in _get_tool_calls_from_message which seems to be working fine. Can PR if that's helpful, but not totally sure what the expected behavior is here.
To Reproduce
Steps to reproduce the behavior:
- init an anthropic service
- init an agent with that service, setting
execution_settings.function_choice_behavior = FunctionChoiceBehavior.Auto() - ask it to do something complex enough that it does >=5 tool calls in a row (or override to 2?)
This one is a little tricker to have happen and haven't pinned down the exact case to repro, but it's something along the lines of
- initialize an AgentGroupChat using an anthropic service
- ask to do something that requires tool use
- on handoff (either to user or to another assistant?) tool calls are serialized (maybe for ChatHistory? not sure) and I think that is likely what forces the issue
In general it seems clear though that FunctionCallContents are intended to be hashable and the AnthropicChatCompletion does not meet that goal. Not clear to me why FunctionCallContent allows a dict for the arguments param (maybe legacy?) .. e.g. type -> arguments: str | dict[str, Any] | None = None
Expected behavior
A clear and concise description of what you expected to happen.
- not totally sure. I think the intended behavior (assuming) is to backoff the agent from self-invoking more than 5 tool calls. Maybe there is a more anthropic-ey way of handling this than just blowing away the tool call part of the payload (assuming I've debugged this correctly)
- somehow the
argumentsparameter needs to be hashable – maybe it's just okay tojson.dumps?
Screenshots
If applicable, add screenshots to help explain your problem.
Platform
- OS: Mac Seqouia 15.1
- Language: Python
- Source: 1.16.0 / using uv
Additional context
Add any other context about the problem here.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status