fix(openai): persist refusal explanations in OpenAIAgent response - #663
Open
shobhitagnihotri69 wants to merge 1 commit into
Open
shobhitagnihotri69 wants to merge 1 commit into
shobhitagnihotri69 wants to merge 1 commit into
Conversation
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
Follow-up to PR #660 (
fix(agents): persist Anthropic refusal explanations). This PR surfaces and persists model refusal explanations inOpenAIAgent, bringing it into full parity withClaudeAgentandOpenAICompatibleAgent.Problem
When OpenAI models refuse a prompt or tool call due to content policies or safety guardrails:
ResponseOutputRefusalitems inmessage.contentwere skipped because onlyResponseOutputTextwas extracted.incomplete_details.reason == "content_filter"was recorded infinish_reason, but therefusalfield was never populated.OpenAIAgent.get_response()returnedrefusal=None, causing downstream evaluation pipelines and benchmark traces to lose the refusal explanation.Solution
ResponseOutputRefusalblocks from message content intorefusal.incomplete_details.reason == "content_filter"and no explicit refusal text was emitted.refusalto the returnedAgentStep.Test Coverage
test_get_response_surfaces_refusal_contentandtest_get_response_surfaces_refusal_on_content_filtertohud/agents/tests/test_openai_agent.py.uv run pytest hud/agents/tests/test_openai_agent.py.Note
Low Risk
Localized response-parsing change with no auth, persistence, or tool-execution behavior changes; mainly improves observability for blocked prompts.
Overview
OpenAIAgent now populates
AgentStep.refusalwhen the Responses API refuses a turn, aligning behavior with the other agents so eval traces keep the explanation.During
get_response, refusal text is read fromResponseOutputRefusal(ortype="refusal") blocks in message content instead of being dropped. If the response ends withincomplete_details.reason == "content_filter"and no refusal text was returned, a standard safety-filter message is used.finish_reasonis unchanged aside from being stored in a local variable before return.Unit tests cover explicit refusal content and the content-filter fallback.
Reviewed by Cursor Bugbot for commit aa8cb13. Bugbot is set up for automated code reviews on this repo. Configure here.