-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Python: Preserve refusals as marked text #7992
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Evan Mattson (moonbox3)
merged 9 commits into
microsoft:main
from
eavanvalkenburg:python-refusal-content
Sep 2, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
941cc01
feat(python): preserve provider refusals
eavanvalkenburg 35f30a9
chore: drop unrelated formatting
eavanvalkenburg 5715b35
refactor(python): preserve refusals as marked text
eavanvalkenburg 60dbe8f
Merge upstream/main into python-refusal-content
eavanvalkenburg c295545
fix(python): preserve refusal content boundaries
eavanvalkenburg 4e4bc65
fix(python): support OpenAI 3 typing floor
eavanvalkenburg 585349a
fix(python): align streamed response indexes
eavanvalkenburg fe8639c
perf(python): bound response index projection
eavanvalkenburg b663293
fix(python): preserve projection edge semantics
eavanvalkenburg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| --- | ||
| status: proposed | ||
| contact: "@eavanvalkenburg" | ||
| date: 2026-09-01 | ||
| deciders: ["@eavanvalkenburg"] | ||
| --- | ||
|
|
||
| # Preserve model refusals with marked Python text content | ||
|
|
||
| ## Context and Problem Statement | ||
|
|
||
| Refusals are provider/model-created output rather than framework execution errors. Python currently | ||
| converts native refusal payloads into ordinary text, which keeps the explanation visible but loses | ||
| the semantic across history, provider replay, Responses-compatible hosting, and DevUI. The framework | ||
| needs a reversible representation without committing prematurely to a new stable content kind. | ||
|
|
||
| Microsoft.Extensions.AI represents refusals as `ErrorContent(ErrorCode="Refusal")`. Python does not | ||
| currently treat refusal output as an error: changing to that model would alter visible-text, | ||
| structured-output, and failure-handling behavior beyond preservation of the provider signal. | ||
|
|
||
| ## Decision Drivers | ||
|
|
||
| - Preserve native refusal semantics through streaming and non-streaming paths. | ||
| - Keep refusal explanations visible through existing message and response text APIs. | ||
| - Round-trip native refusal fields where a transport supports them. | ||
| - Preserve history through existing `Content.to_dict()` and `Content.from_dict()` behavior. | ||
| - Gather usage of the semantic before adding a stable public discriminator. | ||
| - Avoid a parallel content hierarchy or provider-wide parsing abstraction. | ||
|
|
||
| ## Considered Options | ||
|
|
||
| - Keep `type="text"` and record `model_output_kind="refusal"` in `additional_properties`. | ||
| - Add a refusal-only boolean marker to text content. | ||
| - Add a nested model-output metadata mapping to text content. | ||
| - Represent refusals as `ErrorContent`, following Microsoft.Extensions.AI. | ||
| - Add a stable `refusal` discriminator to the unified `Content` model. | ||
|
|
||
| ## Decision Outcome | ||
|
|
||
| Keep refusal explanations as ordinary `Content(type="text", text=...)` and add the experimental, | ||
| serializable marker: | ||
|
|
||
| ```python | ||
| {"model_output_kind": "refusal"} | ||
| ``` | ||
|
|
||
| The flat string value is more general than a refusal-only boolean and cheaper to inspect than a | ||
| nested mapping. It is metadata, not a new public API contract: no `ContentType`, constructor, | ||
| exported constant, or feature-stage entry is added. | ||
|
|
||
| `Message.text`, response/update text, string conversion, and text coalescing remain unchanged. | ||
| Structured-output extraction skips marked refusal text so a refusal is not parsed as the requested | ||
| response model. | ||
|
|
||
| OpenAI Responses, OpenAI Chat Completions, Foundry hosting, Hosting Responses, and DevUI inspect the | ||
| marker to reconstruct native refusal fields, content parts, and streaming events. Other providers | ||
| and protocols require no refusal-specific behavior because the framework content remains text. | ||
|
|
||
| This metadata convention is experimental while usage is gathered. The stable core/OpenAI package | ||
| lifecycle is unchanged because no new public API is introduced; beta and alpha hosting/UI packages | ||
| retain their package-level lifecycle. | ||
|
|
||
| ### Consequences | ||
|
|
||
| - Serialized refusal content keeps the existing text shape and adds | ||
| `additional_properties.model_output_kind="refusal"`. | ||
| - Existing stored refusals remain ordinary text because they carry no reliable migration signal. | ||
| - Older runtimes preserve and render the text and serialize the additional property, but do not | ||
| reconstruct native refusal fields until upgraded. | ||
| - Native providers can reconstruct their refusal wire representation from durable history without | ||
| relying on non-serializable SDK objects. | ||
| - Refusals continue to look like text to middleware and non-native providers. | ||
| - A future decision may promote observed usage to `ErrorContent` semantics or a stable discriminator. | ||
|
|
||
| ### Rejected alternatives | ||
|
|
||
| A boolean marker is slightly shorter but creates a refusal-only key that cannot represent another | ||
| model-output semantic. A nested mapping reserves more structure than the current requirement needs. | ||
| `ErrorContent(ErrorCode="Refusal")` aligns with Microsoft.Extensions.AI but would change Python's | ||
| current visible-text and failure semantics. A stable `refusal` discriminator is clearer and may be | ||
| appropriate later, but commits the public content model before usage and cross-provider behavior are | ||
| understood. |
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.