-
Notifications
You must be signed in to change notification settings - Fork 2.4k
feat: support structured outputs in OpenAIChatGenerator
#9754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 20 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
bf363bb
Add parse for response format
Amnah199 df844b4
Update response_format
Amnah199 cb5f2dc
Add tests
Amnah199 f54826d
Add release notes
Amnah199 4bc8b65
Update checks
Amnah199 1b5ed82
remove instance var
Amnah199 02d3d59
Add tests for azure
Amnah199 060c4f0
Add schema test
Amnah199 c2d41fe
Add comments
Amnah199 26533a2
Merge branch 'main' into openai-structured-outputs
Amnah199 4c0069c
Add streaming support
Amnah199 0600176
Merge branch 'openai-structured-outputs' of https://github.com/deepse…
Amnah199 b123b9a
PR comments
Amnah199 cde0714
PR comments
Amnah199 2b4a0ff
Add tests
Amnah199 0dbe1fd
Fix tests
Amnah199 52401d6
Add unit tests
Amnah199 333b9e8
Update Azure files
Amnah199 6972d77
PR comments
Amnah199 5d50f0a
Small fix
Amnah199 762dca4
Include message.parsed
Amnah199 95707c7
Fix seriliaztion
Amnah199 c7476d4
Update the async method
Amnah199 6c8988d
Update release notes
Amnah199 868faf1
Loosen tests to prevent failure
Amnah199 1931316
PR comments
Amnah199 44b869b
Fix release notes
Amnah199 065262d
Fix error
Amnah199 0c00731
Merge branch 'main' into openai-structured-outputs
Amnah199 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
releasenotes/notes/add-openai-structured-outputs-906be78a984a1079.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
features: | ||
- | | ||
`OpenAIChatGenerator` and `AzureOpenAIChatGenerator` now support structured outputs using `response_format` | ||
parameter that can be passed in `generation_kwargs`. | ||
The `response_format` parameter can be a Pydantic model or a JSON schema for non-streaming responses. For streaming responses, the `response_format` must be a JSON schema. | ||
Example usage of the `response_format` parameter: | ||
```python | ||
from pydantic import BaseModel | ||
from haystack.components.generators.chat import OpenAIChatGenerator | ||
from haystack.dataclasses import ChatMessage | ||
|
||
class NobelPrizeInfo(BaseModel): | ||
recipient_name: str | ||
award_year: int | ||
category: str | ||
achievement_description: str | ||
nationality: str | ||
|
||
client = OpenAIChatGenerator( | ||
model="gpt-4o-2024-08-06", | ||
generation_kwargs={"response_format": NobelPrizeInfo} | ||
) | ||
|
||
response = client.run(messages=[ | ||
ChatMessage.from_user("Give me information about the 20th Nobel Peace Prize winner.") | ||
anakin87 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
]) | ||
print(response) | ||
|
||
Amnah199 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a different way to import this function that doesn't go through a private file? I'm a little worried the import path is subject to break/change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, for now seems like this is the only way to import this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm I think we can do this a different way. We should be able to directly use the one from pydantic which looks like this
parameters_schema = model.model_json_schema()
. This is from the_create_tool_parameters_schema
function in ComponentToolThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OpenAI expect a stricter JSON Schema than Pydantic’s default. For example, objects must set
additionalProperties
andOptional
keys are handled differently. As a result,model_json_schema()
often isn’t accepted as-is.Its also discussed here where another solution is offered but I prefer using the openai method over some unpopular library.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevertheless, I spotted a bug in
to_dict
where schema wasn't stored properly. Fixing this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ahh okay thanks for the info