Skip to content

feat: add JSON mode via response_format parameter#993

Open
eloe wants to merge 2 commits intoBlaizzy:mainfrom
eloe:upstream/json-mode
Open

feat: add JSON mode via response_format parameter#993
eloe wants to merge 2 commits intoBlaizzy:mainfrom
eloe:upstream/json-mode

Conversation

@eloe
Copy link
Copy Markdown

@eloe eloe commented Apr 9, 2026

Summary

Adds response_format support to /v1/chat/completions and /responses, per the OpenAI API spec.

When response_format.type is "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 Behavior
"text" (default) No change
"json_object" Injects JSON-only system instruction

Tests

  • test_resolve_response_format_json_adds_instruction
  • test_resolve_response_format_text_no_change
  • test_resolve_response_format_none_no_change
  • test_responses_json_mode_accepted
python -m pytest mlx_vlm/tests/ -k "json_mode or response_format" -v

eloe and others added 2 commits April 8, 2026 20:32
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant