Skip to content

ToolCall::arguments() throws TypeError when model returns JSON null string #982

@obalaweb

Description

@obalaweb

Bug Report

Summary

ToolCall::arguments() declares : array as its return type but can return
null when the model emits the literal JSON string "null" in the
function.arguments field, causing a TypeError at the call site.

Steps to Reproduce

  1. Register any tool with no required parameters
  2. Use a Groq provider with a Llama model (e.g. llama-3.3-70b-versatile)
  3. Ask the model something that triggers the no-arg tool call
  4. Prism attempts to execute the tool and throws

Error

  TypeError: Prism\Prism\ValueObjects\ToolCall::arguments(): Return value
  must be of type array, null returned

  #0 vendor/prism-php/prism/src/Concerns/CallsTools.php:137
  #1 vendor/prism-php/prism/src/ValueObjects/ToolCall.php:42

Root Cause

Some models emit "arguments": "null" (valid JSON representing a null value)
instead of "arguments": "{}" for no-arg tool calls.

json_decode('null', true) is valid and returns PHP null, which violates
the : array return type.

Expected Behaviour

arguments() always returns an array. JSON null should be treated as
"no arguments" and return [].

Suggested Fix

$decoded = json_decode($arguments, true, flags: JSON_THROW_ON_ERROR);
return is_array($decoded) ? $decoded : [];

Environment

- Provider: Groq
- Model: llama-3.3-70b-versatile
- Prism: v0.99.22
                                                                                                                      
---

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions