feat: add JSON mode via response_format parameter#993
Open
eloe wants to merge 2 commits intoBlaizzy:mainfrom
Open
feat: add JSON mode via response_format parameter#993eloe wants to merge 2 commits intoBlaizzy:mainfrom
eloe wants to merge 2 commits intoBlaizzy:mainfrom
Conversation
Accept OpenAI's response_format: {"type": "json_object"} on both
/chat/completions and /responses endpoints. When json_object is
requested, a system instruction is prepended telling the model to
respond with valid JSON only.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Don't mutate messages list in-place (return new list) - Validate unsupported response_format types → 400 - Add missing asyncio, Request imports - Add missing get_request_timeout/DEFAULT_REQUEST_TIMEOUT - Use asyncio.get_running_loop() instead of get_event_loop() - Add thread-cancel caveat comments on wait_for - Use monkeypatch for env var tests - Add test for unsupported response_format type Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Adds
response_formatsupport to/v1/chat/completionsand/responses, per the OpenAI API spec.When
response_format.typeis"json_object", a system instruction is prepended to the conversation requiring the model to respond with valid JSON only. This is a lightweight, model-agnostic approach that works with any model that follows system instructions.Example
{ "model": "...", "messages": [{"role": "user", "content": "List 3 colors"}], "response_format": {"type": "json_object"} }Supported values
type"text"(default)"json_object"Tests
test_resolve_response_format_json_adds_instructiontest_resolve_response_format_text_no_changetest_resolve_response_format_none_no_changetest_responses_json_mode_accepted