If you have a type like:
public record Product(int Name, decimal Price);
... and make a call like:
var response = await chatClient.CompleteAsync<Product>(
"Extract info about the following: we sell eggs for fifty dollars");
if (response.TryGetResult(out var product))
{
Console.WriteLine($"{product.Name} costs {product.Price:c}");
}
... you would expect it to work quite reliably. But it does not - often the result JSON can't be parsed.
The cause
On debugging, I found this is because the JSON schema we generate contains this:
"price": {
"type": ["string", "number" ]
}
The LLM sees the invitation to supply a string, and often returns JSON like:
{ "Name": "Eggs", "Price": "$50" }
or sometimes
{ "Name": "eggs", "Price": "fifty dollars" }
... neither of which can be JSON-parsed as a Product.
The solution
By default, for decimal/double/float, the schema should not invite the LLM to respond with a string, because suggesting it can return an arbitrary string will allow it to put in literally anything. It has no reason even to favour values that are numeric-like.
@eiriktsarpalis Should we be passing some other option to the JSON schema generator to advise it not to use string for numeric types?
If you have a type like:
... and make a call like:
... you would expect it to work quite reliably. But it does not - often the result JSON can't be parsed.
The cause
On debugging, I found this is because the JSON schema we generate contains this:
The LLM sees the invitation to supply a
string, and often returns JSON like:{ "Name": "Eggs", "Price": "$50" }or sometimes
{ "Name": "eggs", "Price": "fifty dollars" }... neither of which can be JSON-parsed as a
Product.The solution
By default, for
decimal/double/float, the schema should not invite the LLM to respond with astring, because suggesting it can return an arbitrary string will allow it to put in literally anything. It has no reason even to favour values that are numeric-like.@eiriktsarpalis Should we be passing some other option to the JSON schema generator to advise it not to use
stringfor numeric types?