Fix agent-openai-agents-sdk multi-turn failure on 2nd+ prompt (id-less assistant history item) - #256
Open
maskebail wants to merge 1 commit into
Conversation
Apps built from the agent-openai-agents-sdk template fail on the second and
later prompts with:
agents.exceptions.UserError: Unhandled item type or structure: {...
'content': [{'text': ..., 'type': 'output_text'}], 'role': 'assistant',
'type': 'message'}
openai-agents >= 0.19 tightened Converter.maybe_response_output_message in
chatcmpl_converter.py to require {"id","content"} on assistant history items.
The built-in chat UI replays the prior assistant turn as an id-less
{"type":"message","role":"assistant","content":[output_text]} item, and
MLflow's ResponsesAgentRequest strips any client-supplied id during
normalization, so the replayed item fails recognition and falls through to the
catch-all raise. The first prompt has no assistant history to replay, so it
never triggers. The template pins openai-agents>=0.4.1 with no upper bound and
no lockfile, so fresh installs resolve onto a build containing the change.
Fix: collapse replayed id-less assistant output_text items to the easy-input
{"role","content"} form before Runner.run, in both invoke_handler and
stream_handler. That form is recognized by maybe_easy_input_message (no id
required), so the fix is independent of the installed openai-agents version.
Multiple output_text segments are joined with a newline to match the SDK
converter byte-for-byte. The helper lives in a dependency-free
agent_server/history.py so it is unit-testable in isolation.
Note: pinning openai-agents below 0.18 is not a viable workaround -- it
resolves to 0.17.8, whose older model layer is incompatible with the current
openai token-usage schema and fails every prompt with
"InputTokensDetails: cache_write_tokens Field required".
Co-authored-by: Isaac
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
Apps built from the
agent-openai-agents-sdktemplate fail on the second and later prompts with:The first prompt always succeeds; every follow-up fails. Reproduces on fresh installs.
Root cause
openai-agents>= 0.19 tightenedConverter.maybe_response_output_messageinchatcmpl_converter.pyto require{"id", "content"}on assistant history items:Failure chain on the 2nd+ prompt:
{"type":"message","role":"assistant","content":[output_text]}item.ResponsesAgentRequeststrips any client-suppliedidduring normalization, so the item reaching the converter is guaranteed id-less.raise UserError("Unhandled item type or structure").The template pins
openai-agents>=0.4.1with no upper bound and no lockfile, so fresh installs resolve onto a build containing the change.Fix
Collapse replayed id-less assistant
output_textitems to the easy-input{"role","content"}form beforeRunner.run, in bothinvoke_handlerandstream_handler. That form is recognized bymaybe_easy_input_message(no id required), so the fix is independent of the installedopenai-agentsversion. Multipleoutput_textsegments are joined with a newline to match the SDK converter byte-for-byte.The helper lives in a new dependency-free
agent_server/history.pyso it is unit-testable in isolation.Why not just pin
openai-agents?A version pin was considered and rejected — pinning
<0.18resolves to0.17.8, whose older model layer is incompatible with the currentopenaitoken-usage schema and fails every prompt (including the 1st) withInputTokensDetails: cache_write_tokens Field required. The code fix above is the correct, version-independent solution.Validation
Verified on a live Databricks Apps deployment (
openai-agents 0.19.1, guard active):response.completed+[DONE]New offline unit tests (
tests/test_history_normalization.py, 4 cases, no network) lock in the transformation so the regression can't return silently.Changes
agent_server/history.py— new, dependency-freenormalize_history_items()agent_server/agent.py— import + wire both handlers (+3/−2)tests/test_history_normalization.py— regression tests