Skip to content

feat(tool): decode an object or array argument a model sent as a string of JSON - #1047

Open
wishborn wants to merge 1 commit into
prism-php:mainfrom
Particle-Academy:fix/tool-json-string-arguments
Open

wishborn wants to merge 1 commit into
prism-php:mainfrom
Particle-Academy:fix/tool-json-string-arguments

Conversation

@wishborn

Copy link
Copy Markdown
Contributor

Some models send an object-typed tool argument as a string containing JSON. Haiku did, with an object schema argument, and the call failed with Parameter validation error: Type mismatch. Expected: [schema (ObjectSchema, required)].

When a parameter's schema is an ObjectSchema or ArraySchema and the value is a string, Tool now decodes it as JSON and uses it only if it is exactly that JSON type: an object, or a list. Invalid JSON or the wrong JSON type passes through unchanged and fails exactly as before. A string-typed parameter is never decoded, and neither is one whose handler declares string.

It sits beside the scalar and BackedEnum coercion already in coerceArguments(). Tests enter the way CallsTools calls a tool (call_user_func_array($tool->handle(...), $toolCall->arguments())). The decode test fails without the change; the rest are controls. Full suite, phpstan and rector pass.

Third of the smaller PRs out of #1028.

…ng of JSON

Some models send an object-typed tool argument as a string containing
JSON. Haiku did, with an object `schema` argument, and the call failed:
"Parameter validation error: Type mismatch. Expected: [schema
(ObjectSchema, required)]".

When a parameter's schema is an ObjectSchema or ArraySchema and the
value is a string, it is decoded as JSON and used only if it is exactly
that JSON type: an object, or a list. Invalid JSON or the wrong JSON type
passes through unchanged and fails exactly as before. A string-typed
parameter is never decoded, and neither is one whose handler declares
`string`.

Tests enter as CallsTools calls a tool, call_user_func_array on
handle(...) with the model's named arguments. The decode test fails
without the change; the others are controls that pass either way.

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