Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Structured output schema generation is too loose for decimal/double/float #5787

Copy link
Copy link

Description

@SteveSandersonMS
Issue body actions

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?

Reactions are currently unavailable

Metadata

Metadata

Labels

area-aiMicrosoft.Extensions.AI librariesMicrosoft.Extensions.AI librariesbugThis issue describes a behavior which is not expected - a bug.This issue describes a behavior which is not expected - a bug.work in progress 🚧

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

Morty Proxy This is a proxified and sanitized view of the page, visit original site.