Skip to content

OpenRouter ToolCallMap throws TypeError on malformed JSON arguments instead of a handled provider error #1006

@ruttydm

Description

@ruttydm

I found a crash path in prism-php/prism v0.99.22 when mapping OpenRouter tool calls.

Prism\Prism\Providers\OpenRouter\Maps\ToolCallMap::map() does this:

arguments: json_decode((string) $toolCall['function']['arguments'], true),

If the model returns malformed JSON for function.arguments, json_decode(..., true) returns null. That null is then passed to Prism\Prism\ValueObjects\ToolCall, whose constructor expects array|string for $arguments.

The result is a raw PHP TypeError instead of a handled Prism exception.

Minimal repro

Prism\Prism\Providers\OpenRouter\Maps\ToolCallMap::map([
    [
        'id' => '1',
        'function' => [
            'name' => 'demo',
            'arguments' => '{',
        ],
    ],
]);

This throws:

TypeError: Prism\Prism\ValueObjects\ToolCall::__construct(): Argument #3 ($arguments) must be of type array|string, null given

Expected

Malformed tool-call JSON should be handled gracefully, for example by:

  • preserving the raw string, or
  • throwing a Prism-level provider/parsing exception with context

Actual

A low-level TypeError escapes the mapping layer.

Because tool arguments are model-generated, malformed JSON is a normal failure mode and should not crash the mapper this way.

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