Repository navigation
fix(gemini): skip undownloadable history images instead of failing the turn - #10894
Open
JosephTian876 wants to merge 5 commits into
Open
JosephTian876 wants to merge 5 commits into
JosephTian876 wants to merge 5 commits into
Conversation
Contributor
There was a problem hiding this comment.
Hey - I've reviewed your changes and they look great!
Linked issues
#10884 [Bug] OneBot 图片下载失败被误判为模型故障,导致回复中断或触发模型回退 · 3 of 3 met
| # | Requirement | Status | Notes |
|---|---|---|---|
| 1 | Treat failed image downloads as recoverable image-input failures, not model-request failures. | ✅ Met | The media resolver now catches the declared recoverable image errors in non-strict mode, including DownloadFileHTTPError, while preserving propagation for undeclared errors. |
| 2 | Skip only the failed image, preserving the user’s text and other valid images so the model call can proceed. | ✅ Met | Gemini’s payload processing skips a recoverably unavailable image and continues processing the remaining parts; if no parts remain, it adds a placeholder text part. |
| 3 | Do not trigger a fallback provider because an image in the conversation history could not be downloaded. | ✅ Met | The Gemini boundary handles recoverable image errors before they reach the agent runner; the added runner test checks that the primary provider is called and the fallback is not. |
Sourcery assessment
Approved.
camera-2018
approved these changes
Nov 1, 2026
1 task done
to_base64_data only caught OSError and ValueError, but DownloadFileHTTPError and aiohttp ClientError subclass RuntimeError and is_recoverable_image_error already declares them skippable. Catch broadly and keep propagating anything outside that set so non-strict image payload assembly can still skip a bad reference.
Replayed history images were resolved with strict=True, so one unavailable image aborted the whole turn and the runner classified it as a provider failure, switching to a fallback provider. Resolve history images in non-strict mode, log and skip the failed reference, and keep image-only user turns valid with an empty text part.
Non-strict image resolution degraded only DownloadFileHTTPError was pinned. Add coverage for the other declared-recoverable errors (aiohttp ClientError, SyntaxError, EOFError, struct.error) and for errors that must still propagate (OSError ENOMEM, plain RuntimeError).
process_image_url also serves current-turn image parts, so the warning must not claim to skip history, and the exception type tells a failed download apart from a corrupt payload. Resolve strictly and reuse is_recoverable_image_error, the same boundary astr_main_agent uses.
Strict resolution raises instead of returning None today, so the assert narrowing the optional was unreachable. An assert disappears under python -O, so a future None would surface as an AttributeError further down. Branch on None explicitly and reuse the failure path's warning-and-skip behaviour. The added test pins that skip branch.
Contributor
Author
JosephTian876
force-pushed
the
fix/gemini-history-image-degradation
branch
from
November 21, 2026 21:17
be10e5b to
6cfb39c
Compare
Contributor
Linked issuesChecked at commit 6cfb39c #10884 [Bug] OneBot 图片下载失败被误判为模型故障,导致回复中断或触发模型回退 · 4 of 4 met
|
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
This branch has not been deployed
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.
Fixes #10884.
Problem
When a Gemini provider replays conversation history that contains an image
reference (for example an OneBot image URL), a failed download of that single
image is treated as a failure of the main model:
DownloadFileHTTPError, orprovider, even though only one image in the replayed history could not be
materialized.
Root cause
The real failure path is:
Two independent defects stack up on that path:
DownloadFileHTTPError(astrbot/core/utils/io.py) subclassesRuntimeError, and aiohttp'sClientError,SyntaxError,EOFErrorandstruct.errorare likewise notOSError/ValueError.MediaResolver. to_base64_dataonly caught(OSError, ValueError), so the documented"an image that cannot be materialized degrades to
Nonein non-strictmode" behavior never applied to download failures — even though
is_recoverable_image_error()explicitly listsDownloadFileHTTPError.process_image_urlpassedstrict=Trueand turned aNoneresult into aValueError, so even a corrected resolver could not degrade: it wouldnecessarily raise, and the runner's per-candidate
except Exceptiontranslated that into "the model failed".
This is a classification bug, not a design decision. The same repository
already degrades one image at a time in
astr_main_agent.py([Image unavailable]text part and continue), inprovider/entities.py, and inGemini's own
assemble_contextfor the current turn. Only the Gemini historyreplay path and the
media_utilscatch were inconsistent, so no new contractor configuration switch is needed.
Behavior after the fix
MediaResolver.to_base64_data(strict=True)still raises for an image thatcannot be materialized; strict callers are unchanged.
OSError,ValueError,SyntaxError,EOFError,struct.error,ClientError,DownloadFileHTTPError) degrades toNone. Unrecoverable failures such asresource exhaustion (
ENOMEM,EMFILE,ENFILE) and programming errorsstill propagate.
the declared-recoverable errors at its own boundary, the same shape
astr_main_agentuses: one warning carrying the exception type and thelog-safe reference, then skip only that image.
process_image_urlalsoserves current-turn parts, so the wording names the payload rather than the
history. A turn that still has text keeps exactly that text, so replayed
prompt content is unchanged; only a turn left with no parts gets a single
space part. A
Noneresult from strict resolution (not produced today) takesthe same warning-and-skip path through an explicit
is Nonebranch, so theoptional is narrowed without an
assertthatpython -Owould strip.valid, non-empty
UserContent(a single space text part), so the requeststays well-formed.
so the primary provider is used and no fallback provider is switched in.
Why this is safe
Tests
Tests that pin the fix fail on the previous code; the two boundary tests keep
the catch narrow and the recoverability guard in place:
tests/test_media_utils.py:: test_image_base64_data_degrades_for_download_http_error_when_not_strict:a mocked
download_fileraisingDownloadFileHTTPErrordegrades toNonein non-strict mode, cleans up its temp file, and still raises in strict mode.
tests/test_media_utils.py:: test_image_base64_data_degrades_declared_recoverable_errors:ClientError,SyntaxError,EOFErrorandstruct.errorall degrade toNone.tests/test_media_utils.py:: test_image_base64_data_propagates_undeclared_errors:OSError(ENOMEM)and aplain
RuntimeErrorstill propagate. Narrowing the catch toDownloadFileHTTPErrorfails the declared-recoverable test above; droppingthe recoverability guard fails this one.
tests/test_gemini_source.py:: test_gemini_prepare_conversation_skips_history_image_download_failure:a three-message history whose last turn has text plus a failing
image_urlproduces three contents with exactly the text part, and logs the skipped
reference together with the exception type; the type assertion fails without
the warning change.
tests/test_gemini_source.py:: test_gemini_prepare_conversation_keeps_non_empty_user_turn_for_failed_image:an image-only failed turn yields one non-empty
UserContent.tests/test_gemini_source.py:: test_gemini_prepare_conversation_skips_image_resolved_to_no_data: a resolvermonkeypatched to return
Noneskips the image with a warning and keeps thetext part; it fails on the previous
assert-based narrowing.tests/test_tool_loop_agent_runner.py:: test_degraded_history_image_does_not_switch_to_fallback_provider: arunner-level request through a Gemini provider with a failing history image
calls the primary provider once and never the fallback provider.
Verification:
tests/test_media_utils.py,tests/test_gemini_source.py,tests/test_tool_loop_agent_runner.py—167 passed.
uv run python -m pytest -q .): 5050 passed,79 skipped, 3 failed, in 341s. The 3 failures are the pre-existing Windows
managed-shell
TimeoutErrortests intests/test_local_shell_component.py;the same 3 fail on a clean
origin/mastercheckout.ruff format --check .andruff check .pass.Suggested merge order
Logically independent of
#10961,#11185, and#11298(all touch distinct sections ofastrbot/core/provider/sources/gemini_source.py). Suggested order: #10961 → #10894 → #11185 → #11298 (any order works).Summary by Sourcery
Skip recoverable image materialization failures during Gemini conversation preparation without failing the turn or switching providers.
Bug Fixes:
Enhancements:
Tests: