fix(agent): rebuild api_messages after provider fallback to apply reasoning requirements#21033
Open
bradhallett wants to merge 1 commit intoNousResearch:mainfrom
Open
Conversation
…soning requirements When Hermes falls back from one provider (e.g. GLM-5.1) to DeepSeek via _try_activate_fallback(), the api_messages array was stale — built with the original provider's settings where _copy_reasoning_content_for_api may not have added reasoning_content padding. DeepSeek requires reasoning_content on every assistant message in thinking mode, so stale messages cause HTTP 400. Extract _build_api_messages() as an instance method so the same build logic runs both at initial request time and after provider fallback, always with the current provider's settings. Add a call to _build_api_messages() at all 6 _try_activate_fallback() sites in the retry loop to rebuild api_messages before continuing. Fixes NousResearchGH-17212, NousResearchGH-17825, relates to NousResearchGH-13235.
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.
Problem
When Hermes falls back from one provider (e.g. GLM-5.1) to DeepSeek/Kimi via
_try_activate_fallback(), theapi_messagesarray is stale — it was built before the fallback with the original provider's settings. Since_copy_reasoning_content_for_apiruns with the original provider's model, DeepSeek-requiredreasoning_contentpadding is not added to assistant messages.This causes HTTP 400 from DeepSeek:
Root Cause
The message-building block sits outside the inner retry loop. After
_try_activate_fallback()success, thecontinueloops back to the retry loop top — not the message builder — so staleapi_messagesis reused with the wrong provider's settings.Fix
_build_api_messages()— an instance method onAIAgentthat returns(api_messages, total_chars, approx_tokens)._build_api_messages()call at all 6_try_activate_fallback()sites in the retry loop to ensureapi_messagesis rebuilt with the current provider's settings before continuing.Testing
tests/run_agent/test_build_api_messages_fallback.pytest_deepseek_reasoning_content_echo.pytests continue to passRelated