Repository navigation
feat(provider): add response_format, max_tokens, and temperature to text_chat - #11616
Open
JosephTian876 wants to merge 1 commit into
Open
JosephTian876 wants to merge 1 commit into
JosephTian876 wants to merge 1 commit into
Conversation
Contributor
|
Sorry @JosephTian876, you've used your own review budget of 250,000 diff characters for the last 7 days. You can request another review in 4 days and 10 hours by commenting |
…ext_chat
Add optional per-request response_format, max_tokens, and temperature
parameters to Provider.text_chat() and Provider.text_chat_stream(), and
implement provider-specific translation and override precedence across OpenAI
Chat Completions, OpenAI Responses, Google Gemini, and Anthropic Messages:
- Provider base (provider.py): expose optional response_format, max_tokens,
and temperature kwargs on text_chat and text_chat_stream.
- OpenAI Chat Completions (openai_source.py): forward per-request params in
payloads, strip conflicting defaults from custom_extra_body, and honor
request_max_retries on outer retry loops so single-attempt callers fail fast.
- OpenAI Responses (openai_responses_source.py): translate Chat Completions
response_format (json_schema / json_object / text) into text.format, map
max_tokens to max_output_tokens, and preserve non-conflicting
custom_extra_body.text keys (e.g. verbosity).
- Google Gemini (gemini_source.py): translate response_format into
response_mime_type="application/json" and response_schema while stripping
unsupported schema keys (additionalProperties, additional_properties,
$schema), and honor request_max_retries on outer retry loops.
- Anthropic Messages (anthropic_source.py): never forward OpenAI
response_format; use a forced emit_json tool (unwrapped back into
completion_text in both sync and streaming paths, disabling conflicting
thinking config) when no caller tools are active and root schema is an
object, falling back to output_config.format otherwise; route SDK-unsupported
kwargs such as temperature on anthropic v2 into extra_body.
- Tests: add concise unit and wire-level coverage directly in existing
tests/test_{openai,openai_responses,gemini,anthropic}_source.py.
JosephTian876
force-pushed
the
feat/provider-text-chat-structured-output
branch
from
December 4, 2026 04:14
7a2f9fe to
992415d
Compare
This branch has not been deployed
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 optional per-request
response_format,max_tokens, andtemperatureparameters toProvider.text_chat()andProvider.text_chat_stream(), with provider-native structured output translation, per-request override precedence over staticcustom_extra_bodydefaults, and fail-fast outer retry handling whenrequest_max_retriesis specified.Motivation
Plugins and core workflows that invoke
provider.text_chat(...)for structured JSON classification, extraction, or deterministic short replies currently have no uniform per-request way to specifyresponse_format,max_tokens, ortemperatureacross providers:Provider.text_chat()andProvider.text_chat_stream()did not exposeresponse_format,max_tokens, ortemperaturein their base contract.custom_extra_body(such as hightemperatureor largemax_tokens) could conflict with or override per-request structured calls.response_format, OpenAI Responsestext.format, Google Geminiresponse_mime_type+response_schemawithoutadditionalProperties, Anthropic forcedemit_jsontool /output_config.formatwithout an OpenAIresponse_formatkey).ProviderOpenAIOfficialandProviderGoogleGenAIhardcoded 10 retries even when callers explicitly requestedrequest_max_retries=1for fail-fast structured calls.Changes
Base Provider Interface (
astrbot/core/provider/provider.py)response_format: dict | None = None,max_tokens: int | None = None, andtemperature: float | None = NonetoProvider.text_chat()andProvider.text_chat_stream()with docstrings, preserving full backward compatibility.OpenAI Chat Completions (
astrbot/core/provider/sources/openai_source.py)response_format,max_tokens, andtemperaturefromtext_chat()/text_chat_stream()through_prepare_chat_payload()intopayloads.response_format,max_tokens, andtemperaturekeys fromextra_bodyin_query()and_query_stream()when explicitly provided per request.text_chat()andtext_chat_stream()tomax(1, int(request_max_retries))whenrequest_max_retriesis provided, and tracks stream completion cleanly so single-attempt calls fail fast on error and never falsely raise on attempt-1 success.OpenAI Responses API (
astrbot/core/provider/sources/openai_responses_source.py)response_format(json_schema,json_object,text) via_translate_response_format()into Responses APItext.format.max_tokenstomax_output_tokensand forwardstemperature._merge_request_overrides()in_query()and_query_stream()so explicit per-requesttext.format,max_output_tokens, andtemperaturetake precedence overcustom_extra_bodywhile preserving non-conflictingcustom_extra_body.textkeys (e.g.verbosity).Google Gemini (
astrbot/core/provider/sources/gemini_source.py)response_formatin_prepare_query_config()via_apply_response_format():json_object->response_mime_type="application/json".json_schema->response_mime_type="application/json"+response_schemawith_strip_additional_properties()recursively stripping unsupported schema keywords (additionalProperties,additional_properties,$schema) without mutating caller dicts or stripping property keys named"additionalProperties"inside"properties".max_tokens(mapped tomax_output_tokens) andtemperaturewith per-request precedence overcustom_extra_body.text_chat()andtext_chat_stream()tomax(1, int(request_max_retries))whenrequest_max_retriesis set.Anthropic Messages (
astrbot/core/provider/sources/anthropic_source.py)response_formatkey (from either request kwargs orcustom_extra_body) to Anthropic Messages API.response_formatvia_apply_response_format():json_schemaorjson_object, injects a forcedemit_jsontool (tool_choice={"type": "tool", "name": "emit_json"}), disables conflictingthinkingconfiguration (from both provider config andcustom_extra_body), and unwraps theemit_jsontool_use payload cleanly back intocompletion_text(preferring the structured JSON payload over any stray preamble text) across both_query()and_query_stream().output_config.format(merging with any non-conflictingcustom_extra_body.output_configkeys).temperatureonanthropic>=2.xoroutput_configon older SDK versions) intoextra_bodyvia_route_unsupported_sdk_params()so wire payloads remain compatible across Anthropic SDK versions without raisingTypeError.Tests (No New Standalone Test Files)
tests/test_openai_source.py,tests/test_openai_responses_source.py,tests/test_gemini_source.py,tests/test_anthropic_source.py), keeping zero new standalone test files in compliance with#11511.Files Changed
astrbot/core/provider/provider.pyastrbot/core/provider/sources/openai_source.pyastrbot/core/provider/sources/openai_responses_source.pyastrbot/core/provider/sources/gemini_source.pyastrbot/core/provider/sources/anthropic_source.pytests/test_openai_source.pytests/test_openai_responses_source.pytests/test_gemini_source.pytests/test_anthropic_source.pyVerification
python -m py_compileon all 5 modified provider source files: passed.ruff format --check .: passed (606 files already formatted).ruff check .: passed (All checks passed!).pytest tests/test_openai_source.py tests/test_openai_responses_source.py tests/test_gemini_source.py tests/test_anthropic_source.py -q: 145 passed, 0 failed (including realhttpx.MockTransportwire SSE test onanthropic==2.16.0).pytest tests/test_provider_user_agent.py -q: 44 passed, 1 skipped.git merge-tree --write-treeagainst base482f5b44aand all 8 siblingAstrBotPR branches: 0 conflicts.建议合并顺序 (Recommended Merge Order)
git merge-tree --write-treeagainst base482f5b44aand all siblingAstrBotPR branches):fix/plugin-service-closed-connectorfix/t2i-local-skip-external-probefix/aiocqhttp-at-member-fallbackfix/system-one-decision-float32-softmax-tolfix/plugin-view-iframe-allow-popupsfix/gemini-thinking-camelcase-and-safetyfix/gemini-openai-extra-body-guardfeat/provider-text-chat-structured-output(this PR) — lands cleanly in any order; placing it after the two Gemini/OpenAI config fixes keeps provider changes easy to review sequentially.