Function calls do not work on a specific system
At some point, magentic started working differently on different systems. Function calls are not working on my system:
[SystemMessage('You are server cleaner.'), UserMessage('Clear server 123')]
[SystemMessage('You are server cleaner.'), UserMessage('Clear server 123'), AssistantMessage('<think>\nOkay, the user wants to clear server 123. Let me check the available tools. There\'s a function called clr_server that takes a server_name parameter. The required parameter is server_name, which is a string. The user provided "123" as the server name. So I need to call clr_server with server_name set to "123". I should make sure the arguments are correctly formatted as JSON within the tool_call tags. No other functions are available, so that\'s the only one to use. Alright, I\'ll structure the response with the function name and the arguments.\n</think>\n\n')]
Traceback (most recent call last):
File "D:\ollamatest\mag1.py", line 31, in <module>
chat = chat.exec_function_call()
^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\ollamatest\test\Lib\site-packages\magentic\_chat.py", line 149, in exec_function_call
raise TypeError(msg)
TypeError: Last message is not a function call.
But everything works fine on my comrade's system.:
[SystemMessage('You are server cleaner.'), UserMessage('Clear server 123')]
[SystemMessage('You are server cleaner.'), UserMessage('Clear server 123'), AssistantMessage(FunctionCall(<function clr_server at 0x00000268825F3D80>, '123'))]
Server 123 has been cleared!
None
[SystemMessage('You are server cleaner.'), UserMessage('Clear server 123'), AssistantMessage(FunctionCall(<function clr_server at 0x00000268825F3D80>, '123')), FunctionResultMessage(None, FunctionCall(<function clr_server at 0x00000268825F3D80>, '123'))]
I tried reinstalling the library, reinstalling the model, changing the virtual environment, changing the location of the script. We have absolutely no idea why it works so strangely.
A few suggestions
- The failed request shows you're using a reasoner as a model. Does that reasoner support tool use at all? If yes - prompt it that it "must use tools" in the system prompt
- Because you're using ollama - make sure that the ollama version is consistent between the machines and the models you are using on each machine match
- Use the
max_retriesparameter when you are making the call to the model. It's a nice way to give the model a way to self-heal (see https://magentic.dev/retrying/#llm-assisted-retries)
Thanks @piiq ! I would also suggest making sure ollama is updated to the latest version. And to help debug you can turn on logs / use logfire to see the raw response from the model: https://magentic.dev/logging-and-tracing/
I also tried running this code, initially everything worked correctly, but it stopped working after updating the openai library. Going back to version 1.84.0 did not help
from magentic import (
AssistantMessage,
Chat,
FunctionCall,
chatprompt,
UserMessage,
SystemMessage,
OpenaiChatModel,
)
import logfire
logfire.configure()
logfire.instrument_openai()
def clr_server(server_name : str):
"""Clear server."""
print(f"Server {server_name} has been cleared!")
chat = Chat(
messages=[SystemMessage("You are server cleaner. You must use tools")],
functions=[clr_server],
output_types=[str, FunctionCall],
model=OpenaiChatModel("qwen3:14b", base_url="http://localhost:11434/v1/", api_key = -1)
)
chat = chat.add_user_message("Clear server 123")
print(chat.messages)
chat = chat.submit()
print(chat.messages)
chat = chat.exec_function_call()
print(chat.last_message.content)
print(chat.messages)