Skip to content

ToolCalling actions are no longer being recognised from model response #992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

sysradium
Copy link
Contributor

@sysradium sysradium commented Mar 15, 2025

@aymeric-roucher

I think it has been another ToolCallingAgent related regression coming from:

e432d41#diff-0319e64c7d0f9e302a7c7a0dd60f04dedb7d4dcf7dbe9e046543335654a9efa1R893

Looks like you made your mind to remove ToolCallingAgent support :)

Basically if a model returns Action:\n{} it does not get mapped into tool_calls here https://github.com/huggingface/smolagents/blob/main/src/smolagents/models.py#L987-L987

And hence it keeps indefinitely failing here: https://github.com/huggingface/smolagents/blob/main/src/smolagents/agents.py#L1066-L1066

image

I came across this problem by playing with:

from smolagents import (
    LiteLLMModel,
    ToolCallingAgent,
    VisitWebpageTool,
)


model = LiteLLMModel(model_id="lm_studio/meta-llama-3.1-8b-instruct", api_base="http://127.0.0.1:1234/v1", api_key="foo")

search_agent = ToolCallingAgent(
    tools=[VisitWebpageTool()],
    model=model,
    name="search_agent",
    description="This is an agent that can do web search.",
)

manager_agent = ToolCallingAgent(
    tools=[],
    model=model,
    managed_agents=[search_agent],
)
manager_agent.run(
    "What is the current record of the New York Knicks NBA team?  Feel free to search the web for the answer."
)

@sysradium sysradium force-pushed the tool-calling-agent-action-extraction-fix branch from bac939e to 2c4bdc8 Compare March 15, 2025 20:21
@sysradium sysradium changed the title For TooCallingAction fix extraction of function arguments from response ToolCalling actions are no longer being recognised from model response Mar 15, 2025
@aymeric-roucher
Copy link
Collaborator

aymeric-roucher commented Mar 16, 2025

@sysradium e432d41#diff-0319e64c7d0f9e302a7c7a0dd60f04dedb7d4dcf7dbe9e046543335654a9efa1R893 did not change the LiteLLM class, so the issue must date back before: it seems to affect all models where the underlying API does not properly return tool calls, where tool calls must be "manually" parsed.
Thus this issue should be fixed by #991. Let's wait for review and I'll do a patch release!

@sysradium sysradium force-pushed the tool-calling-agent-action-extraction-fix branch from 2c4bdc8 to a789575 Compare March 23, 2025 21:04
@sysradium
Copy link
Contributor Author

@aymeric-roucher largely the PR became redundant. However I decided to update a few things still.

The model quite often jumps between arguments and parameters when returning a json for tool calling. Currently it is a fixed key, but I made tool_arguments_keys a list.

Also before json parsing used to fail if the remainder of the output not only contained some garbage, but hat an invalid json payload, like:

Action:
{
"name": "final_answer",
"parameters": {
    "answer": "The current record of the New York Knicks NBA team is not provided in the given functions."
    }
}

Action
}looks like valid, but invalid{

My version fixes that.

@sysradium sysradium force-pushed the tool-calling-agent-action-extraction-fix branch from a789575 to 70a26a6 Compare March 25, 2025 21:27
@sysradium
Copy link
Contributor Author

Sorry, rebased once again instead of merging the main in, as asked by @albertvillanova before.

@@ -228,16 +231,23 @@ def get_clean_message_list(
return output_message_list


def get_tool_call_from_text(text: str, tool_name_key: str, tool_arguments_key: str) -> ChatMessageToolCall:
def get_tool_call_from_text(
text: str, tool_name_key: str, tool_arguments_key: Union[list[str], str]
Copy link
Collaborator

Choose a reason for hiding this comment

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

if I understood well the last argument should betool_arguments_keys instead of tool_arguments_key

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had to keep the name not to break anything. As you can see it's either a list or a single value

break

if end_index is None:
raise ValueError("No complete JSON object found: braces are not balanced.")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Interesting to use parity for this! It seems the only case where this message would appear i when the brace count went up and did never go down to zero, i.e. more braces were opened than closed: maybe mention it, as in "caution: not all opened braces were closed?"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I improved the error reporting as well as the algorithm a bit and added a few more test-cases.

@sysradium sysradium force-pushed the tool-calling-agent-action-extraction-fix branch from 70a26a6 to 1aed4d5 Compare April 10, 2025 21:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants