Skip to content

fix: encode empty tool properties as JSON object instead of array - #1038

Open
heyzainali wants to merge 1 commit into
prism-php:mainfrom
heyzainali:fix/empty-tool-properties-as-object
Open

heyzainali wants to merge 1 commit into
prism-php:mainfrom
heyzainali:fix/empty-tool-properties-as-object

Conversation

@heyzainali

Copy link
Copy Markdown

Description

If a tool has no parameters, Groq, Mistral, xAI and Z send "properties": [] in the tool definition. An empty PHP array goes through json_encode as [], but these APIs expect an object there, so the request gets rejected.

$tool = (new Tool)
    ->as('get_schema')
    ->for('Returns the current schema')
    ->using(fn (): string => '...');

Same root cause as #896, just on the tool definition side instead of the tool call arguments.

Anthropic and OpenRouter already handle this in their own ToolMaps, each a bit differently. Rather than add the same check to four more maps, I added Tool::parametersAsJsonObject(), which returns the parameters, or an empty stdClass when there aren't any, and used it in all six. Output for Anthropic and OpenRouter doesn't change. The Anthropic diff looks bigger than it is because Rector turned the closure into an arrow fn once the local variable was gone.

OpenAI and DeepSeek drop parameters entirely for tools without any, so I left them alone.

Tests: a unit test for the new method, a regression test in the Groq, Mistral and xAI ToolTest, and a new ToolTest for Z since it didn't have one.

#977 fixes xAI on its own. I've included xAI here so all four land together, but happy to drop it if you'd rather merge that one.

This branch has not been deployed

No deployments
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.

1 participant