Repository navigation
Structured output: let an array property declare a scalar item type (e.g. list<int>)
#714
stevechilds
started this conversation in
Ideas
Replies: 1 comment 2 replies
|
Released in the latest 4.1.29 extending the support of |
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Structured output has no way to ask the model for an array of integers (or of numbers or booleans). In
JsonSchema::processProperty(), as of 4.1.27, anyarrayproperty that has noanyOfgetsitems: {"type": "string"}:So the model is explicitly told to return strings, and it does:
produces
and the response comes back as
["3", "7"]. Because the Deserializer doesn't check array items, those strings go through to the PHP object unchanged.What I tried
#[SchemaProperty(anyOf: ['int'])]: a singleanyOfmember goes togenerateClassSchema(), which callsnew ReflectionClass('int')and throws aReflectionException.@var int[]/@var list<int>: docblocks are not read when the schema is built.anyOfworks and gives{"type": "integer", "enum": [...]}, but only when the set of values is fixed and known in advance. That doesn't fit ids, counts or line numbers.At the moment the only option is to cast the values after deserialisation, but by then the schema has already told the model the wrong type.
Proposal
Add an optional item-type argument to
SchemaPropertyfor scalar items, for example:It would accept the JSON Schema scalar types (
string,integer,number,boolean), withstringas the default so existing schemas are unchanged.processProperty()would emititems: {"type": "<value>"}when it is set. Optionally, the Deserializer could cast or validate the items to match.Another route would be to allow scalar names in a single-member
anyOf(anyOf: ['int']). A dedicated argument seems clearer, though, becauseanyOfotherwise means a class or enum.All reactions