fix(openai): align text.format with tool schema to prevent empty {} args in RESPONSES_TOOLS#2304
Open
ayushh0110 wants to merge 3 commits into
Open
fix(openai): align text.format with tool schema to prevent empty {} args in RESPONSES_TOOLS#2304ayushh0110 wants to merge 3 commits into
ayushh0110 wants to merge 3 commits into
Conversation
When using Mode.RESPONSES_TOOLS with create_partial streaming, the OpenAI Responses API emits ResponseReasoningSummaryTextDeltaEvent and ResponseReasoningSummaryTextDoneEvent events that were silently dropped. Add an on_event callback parameter that forwards these non-JSON events to the caller instead of discarding them. The callback is threaded through the full pipeline: patch -> retry -> process_response -> extract_json. - Support both sync and async callbacks in the async streaming path - Pop on_event from kwargs early to prevent it leaking to the OpenAI API - Add 7 unit tests (no API key required) with zero regressions Closes 567-labs#2291
…fix/streaming-reasoning-events
…rgs in RESPONSES_TOOLS
When RESPONSES_TOOLS mode forces a tool call alongside a mismatched
text.format json_schema, the model receives competing output constraints
and may deprioritize tool arguments to '{}'. This is especially common
with low-reasoning models like gpt-5-nano.
Changes:
- Add _ensure_text_format_config() that sets text.format to a json_schema
matching the tool definition's parameters schema (from pydantic_function_tool),
aligning both output paths per OpenAI's Responses API migration guide.
- Apply to both handle_responses_tools and handle_responses_tools_with_inbuilt_tools.
- Add targeted retry message in reask_responses_tools when empty arguments
are detected, explicitly instructing the model to populate required fields.
- Add diagnostic logger.warning in parse_responses_tools when empty tool
arguments are returned.
Closes 567-labs#2300
27b9a49 to
b7769cd
Compare
3 tasks
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
Fixes #2300
When
RESPONSES_TOOLSmode forces a tool call alongside a mismatchedtext.formatjson_schema, the model receives competing output constraints and may deprioritize tool arguments to'{}'. This is especially common with low-reasoning models (e.g.gpt-5-nano).Per OpenAI's Responses API migration guide §6,
text.formatis the Responses API equivalent ofresponse_formatfor structured outputs. Rather than stripping thetextconfig, this PR alignstext.formatwith the tool schema so both output paths use the samejson_schema, giving the model a single consistent signal.Changes
instructor/providers/openai/utils.py_ensure_text_format_config()— Setstext.formatto ajson_schemamatching the tool definition'sparametersschema (frompydantic_function_tool). If a user provides a matching text config, it's preserved; if conflicting, it's overridden with a debug log.handle_responses_tools()— Calls_ensure_text_format_config()after building the tool definition to align both output paths.handle_responses_tools_with_inbuilt_tools()— Same treatment for consistency.reask_responses_tools()— Detects empty'{}'arguments and sends a targeted retry message ("You MUST populate ALL required fields") instead of the generic "fix the errors with {}".instructor/processing/function_calls.pyparse_responses_tools()— Logs aWARNINGwhen tool arguments are empty, helping users identifytext.format/tool_choiceconflicts or insufficient reasoning budget.tests/test_openai_responses_tools.pyAdded 8 unit tests covering:
text.formatis auto-set with correctjson_schema(includingadditionalProperties: false)text.formatis overriddentext.formatis preservedresponse_model=Nonedoesn't crash or set text configtext.formatWhy
parameters_schemainstead ofmodel_json_schema()?OpenAI's strict mode requires
additionalProperties: falsein the schema. Pydantic'smodel_json_schema()doesn't include this, butpydantic_function_tool()does. By reusing the tool'sparametersschema, bothtext.formatand the tool definition share the exact same schema — correctly formatted for strict mode.Test Results