feat: support structured outputs (response_format) in chat completions#43
Open
giwaov wants to merge 1 commit intoOpenGradient:mainfrom
Open
feat: support structured outputs (response_format) in chat completions#43giwaov wants to merge 1 commit intoOpenGradient:mainfrom
giwaov wants to merge 1 commit intoOpenGradient:mainfrom
Conversation
Wire the OpenAI-compatible response_format parameter through the chat completion pipeline: - Bind response_format to LangChain model via model.bind() for json_object and json_schema types (text is a no-op) - Apply to both streaming and non-streaming code paths - Include response_format in the canonical request dict so TEE hashing covers the requested output format - Add 14 unit tests covering parsing, hash-dict serialization, model binding, and interaction with tool calling Closes OpenGradient#14
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
Implements OpenAI-compatible structured outputs support by wiring the
response_formatparameter through the chat completion pipeline, as requested in #14.Changes
tee_gateway/controllers/chat_controller.py_create_non_streaming_response): After tool binding, checksresponse_format. If the type isjson_objectorjson_schema, binds it to the LangChain model viamodel.bind(response_format=...). Thetexttype is a no-op (default behavior)._create_streaming_response): Identical logic applied after tool binding._chat_request_to_dict): Includesresponse_formatin the canonical serialized dict so the TEE signature covers the requested output format.tests/test_structured_outputs.py14 unit tests covering:
response_formatfrom request dicts (text, json_object, json_schema, and absent)bind_toolsandbind(response_format=...)chain correctly)Design Decisions
llm_backend.py: Theresponse_formatis bound per-request viamodel.bind()after retrieving the cached model, following the same pattern already used for tool binding. This keeps the LRU cache clean (keyed only on model/temperature/max_tokens).response_formatdict is forwarded as-is to LangChain, which handles provider-specific translation. This maintains OpenAI API compatibility and works with all supported providers (OpenAI, Anthropic, Google, xAI).Supported Formats
Per the OpenAPI spec already defined in the repo:
{type: text}plain text (default, no-op){type: json_object}JSON mode{type: json_schema, json_schema: {name: ..., schema: {...}, strict: true}}strict schema-constrained outputCloses #14