[integrations][python] Apply DashScope native structured output - #1090
Open
weiqingy wants to merge 1 commit into
Open
[integrations][python] Apply DashScope native structured output#1090weiqingy wants to merge 1 commit into
weiqingy wants to merge 1 commit into
Conversation
TongyiChatModelConnection.chat refused an output schema outright, because
the connection had no native translation. DashScope does support
schema-enforced structured output, so translate an OutputSchema into the
provider's response_format={"type": "json_schema", ...} parameter.
Capability is reported only for models that can honor it. DashScope
documents json_schema for five model families, but four of them are served
on the multimodal interface while this connection calls Generation.call on
the text-generation interface, and a mismatched pairing answers "url error".
The Qwen3.7-Max family is that intersection. Every other model, including
the qwen-plus default, sends no response_format and keeps the existing
prompt-engineered fallback rather than raising.
The translation resolves the schema payload, then refuses a caller-supplied
response_format that would be overwritten, then renders. Rendering earlier
would mask the conflict behind an unrenderable-schema TypeError; refusing
earlier would raise on a RowTypeInfo payload that was never going to be
translated.
The schema is rendered with bare pydantic through render_output_schema
rather than the OpenAI-family helper, which rewrites required to include
every property to satisfy a rule DashScope does not impose.
Generated-by: Claude Code 2.1.251 (Claude Opus 5)
Collaborator
Author
|
The one failing check is unrelated to this PR:
This PR only changes Python and docs. It touches no I have re-triggered the failed job. |
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.
Linked issue: #280
Purpose of change
Passing an
output_schemato the Tongyi connection raisedNotImplementedError, because the connection had no native translation. DashScope does support schema-enforced structured output, so this adds it.The catch is that it applies to fewer models than you might expect. DashScope documents
json_schemafor five model families, but four of them are served on its multimodal interface, while this connection callsGeneration.callon the text-generation interface. Calling one through the other fails withurl error. That leaves the Qwen3.7-Max family as the only place native enforcement is reachable here.So the connection reports the capability for that family, and for everything else, including the
qwen-plusdefault, it sends nothing new and keeps the existing prompt-based fallback. No behavior changes for anyone not on Qwen3.7-Max.Sources for the two halves of that intersection, both worth checking against the allowlist in the diff:
json_schemaHeads-up on the docs: the DashScope API reference pages still list only
textandjson_objectforresponse_formatand never mentionjson_schema. The feature guide above is the current source, and the error reference corroborates it by namingjson_schemaas a recognised key.Tests
New file
test_tongyi_native_structured_output.py, 16 cases, no API key or network needed. It asserts the request the SDK would actually receive: the parameter is present and correctly shaped on a capable model, absent on every other model, absent when no schema is passed, and a non-Pydantic schema falls back instead of failing. The capability check is tested against capable names, incapable names, a lookalike (qwen3.7-maximum), empty string andNone.Each assertion was verified by mutating the source and confirming the intended test fails, so the tests are not passing by accident.
One limitation, stated plainly. No vendor source shows
json_schemaonGeneration.call. It is documented as a parameter of the native API, with the placement rule for HTTP callers, but the only native sample usesMultiModalConversation.callwith a model this connection excludes. Every test here is a mock and I had no DashScope key, so the suite cannot settle it. What is established: the parameter provably reaches the request body, and if the provider rejects it the call fails loudly with aRuntimeErrorrather than silently returning an unconstrained answer. If a reviewer has a key, one live call againstqwen3.7-maxwould close this.API
No public API change.
supports_native_structured_outputis an existing hook this connection now overrides, and no dependency changes. Python only, since there is no Java Tongyi connection.Documentation
doc-neededdoc-not-neededdoc-includedWas this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code 2.1.251 (Claude Opus 5)