Fix ValueError when parsing empty arguments in tool calls #102
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue Description
FastAgent crashes with the following error when a tool call contains empty arguments:
The error occurs in the OpenAI completion function when trying to parse empty arguments from a tool call:
When
tool_call.function.argumentsis an empty string, thefrom_jsonfunction frompydantic_corethrows an error because it can't parse an empty string as valid JSON. This is a general issue that can affect any tool call with empty arguments, not just specific tools.Fix Implementation
This PR adds a check for empty arguments before calling
from_json. If the arguments string is empty or contains only whitespace, it returns an empty dictionary{}instead of trying to parse it:This ensures that when the model generates a tool call with empty arguments, FastAgent passes an empty dictionary to the MCP server instead of throwing an error.
Steps to Reproduce
This issue can be reproduced in any scenario where a model might generate a tool call with empty arguments. One specific way to reproduce it is:
In fastagent.config.yaml:
Steps to Verify
src/mcp_agent/llm/providers/augmented_llm_openai.pyAdditional Context
This is a defensive programming approach that makes the code more robust by handling edge cases gracefully. It allows tool calls to proceed even when the model generates empty arguments, and then the MCP server can handle the validation of required parameters.
Fixing this in FastAgent rather than in individual MCP servers is a more robust approach because: