Skip to content

Conversation

@SteveSandersonMS
Copy link
Member

@SteveSandersonMS SteveSandersonMS commented Oct 23, 2024

Continuation of #5522. I had to start a new PR because #5522 has complicated merge conflicts and after fixing them, I can't rebase and push because it's on a branch. However this is still primarily the work of @kzu (thanks!).

Microsoft Reviewers: Open in CodeFlow

kzu and others added 4 commits October 23, 2024 16:01
Structured outputs in OpenAI require an `object` schema object. By detecting this situation and wrapping always in a `Payload<T>(T Data)` record, we significantly improve the developer experience by making the API transparent to that limitation (which might even be a temporary one?).

The approach works for both OpenAI as well as Azure Inference without native structured outputs. In order to signal a wrapped result to the `ChatCompletion<T>`, we use the `AdditionalProperties` dictionary with a non-standard `$wrapped` property which is a typical convention for JSON properties that are not intended for end-user consumption (like $schema).

Fixes #5521
Rather than relying on the type system, since a source-generated serializer options would not be able to deal with it.
@kzu
Copy link
Contributor

kzu commented Oct 23, 2024

So the decision was to move forward with this? Yay! 🚀

@SteveSandersonMS
Copy link
Member Author

SteveSandersonMS commented Oct 23, 2024

So the decision was to move forward with this? Yay! 🚀

Yeah, I'd like to go ahead optimistically. It seems to work reliably on OpenAI models. On (say) llama3.1 on Ollama, it's at least as reliable as if you defined a wrapper manually, so we're not losing anything by doing this. It is definitely a nice feature to have, making structured output more natural.

If @stephentoub or @eiriktsarpalis think otherwise, please say so.

@stephentoub stephentoub added the area-ai Microsoft.Extensions.AI libraries label Oct 23, 2024
…tStructuredOutputExtensions.cs

Co-authored-by: Eirik Tsarpalis <[email protected]>
@SteveSandersonMS SteveSandersonMS merged commit 46d5e57 into main Oct 24, 2024
@SteveSandersonMS SteveSandersonMS deleted the stevesa/structured-output-improvements branch October 24, 2024 12:10
@github-actions github-actions bot locked and limited conversation to collaborators Nov 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-ai Microsoft.Extensions.AI libraries

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants