-
Notifications
You must be signed in to change notification settings - Fork 607
[FEAT] Support JSON Schema in Responses #1177
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
Open
riedgar-ms
wants to merge
26
commits into
Azure:main
Choose a base branch
from
riedgar-ms:riedgar-ms/selfask-scorer-fix-01
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+382
−79
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
073cf62
Drafting a quick test script
riedgar-ms 244f2cf
Corrected JSON support
riedgar-ms 9fccc5b
Expand testing
riedgar-ms dd36ea2
Don't need this
riedgar-ms 170b16b
Some small refinements
riedgar-ms dd56600
Draft unit test updates
riedgar-ms 8df25b9
Merge remote-tracking branch 'origin/main' into riedgar-ms/selfask-sc…
riedgar-ms 5405dec
Merge remote-tracking branch 'origin/main' into riedgar-ms/selfask-sc…
riedgar-ms fa4ca37
Proposal for schema smuggling
riedgar-ms 7390271
Merge remote-tracking branch 'origin/main' into riedgar-ms/selfask-sc…
riedgar-ms 320d58c
Linting issues
riedgar-ms 842cd03
Merge remote-tracking branch 'origin/main' into riedgar-ms/selfask-sc…
riedgar-ms 80e8cd4
Merge remote-tracking branch 'origin/main' into riedgar-ms/selfask-sc…
riedgar-ms 2003466
Merge remote-tracking branch 'origin/main' into riedgar-ms/selfask-sc…
riedgar-ms 45cb825
Add the JSONResponseConfig class
riedgar-ms ec4efaa
Better name
riedgar-ms 1eb4395
Start on other changes
riedgar-ms 29fdb2f
Next changes
riedgar-ms 2c8e919
Try dealing with some linting
riedgar-ms 9009edf
More changes....
riedgar-ms d899af4
Correct responses setup
riedgar-ms c78f819
blacken
riedgar-ms 45f73a6
Fix a test....
riedgar-ms becb214
Fix reponses tests
riedgar-ms f37d070
Fix chat target tests
riedgar-ms 6072ae3
blacken
riedgar-ms 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from dataclasses import dataclass | ||
| from typing import Any, Dict, Optional | ||
|
|
||
|
|
||
| @dataclass | ||
| class JsonResponseConfig: | ||
| enabled: bool = False | ||
| schema: Optional[Dict[str, Any]] = None | ||
| schema_name: str = "CustomSchema" | ||
| strict: bool = True | ||
|
|
||
| @classmethod | ||
| def from_metadata(cls, *, metadata: Optional[Dict[str, Any]]) -> "JsonResponseConfig": | ||
| if not metadata: | ||
| return cls(enabled=False) | ||
|
|
||
| response_format = metadata.get("response_format") | ||
| if response_format != "json": | ||
| return cls(enabled=False) | ||
|
|
||
| schema_val = metadata.get("json_schema") | ||
| if schema_val: | ||
| if isinstance(schema_val, str): | ||
| try: | ||
| schema = json.loads(schema_val) if schema_val else None | ||
| except json.JSONDecodeError: | ||
| raise ValueError(f"Invalid JSON schema provided: {schema_val}") | ||
| else: | ||
| schema = schema_val | ||
|
|
||
| return cls( | ||
| enabled=True, | ||
| schema=schema, | ||
| schema_name=metadata.get("schema_name", "CustomSchema"), | ||
| strict=metadata.get("strict", True), | ||
| ) | ||
|
|
||
| return cls(enabled=True) |
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
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
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.
This is the 'bug fix' part;
response_formatis from the Chat completions API. See:https://platform.openai.com/docs/api-reference/responses/create#responses_create-text