Repository navigation
fix(gemini): deliver a video that arrives through the audio slot - #11018
JosephTian876 wants to merge 2 commits into
Conversation
|
Sorry @JosephTian876, you've used your own review budget of 250,000 diff characters for the last 7 days. You can request another review in 1 day and 12 hours by commenting |
|
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
f3d8ea7 to
6b5b91c
Compare
|
Rebased onto updated #10202 ( |
6b5b91c to
8db94be
Compare
|
Rebased onto the updated Local Verification
建议合并顺序
|
8db94be to
7b5d2d7
Compare
fix(gemini): deliver a video that arrives through the audio slot
Background
ProviderGoogleGenAIhas two request shapes, and only one of them treats a video asa video.
The agent runner calls
text_chat(contexts=…, extra_user_content_parts=…)without aprompt(tool_loop_agent_runner.py:529-539). That shape assembles its blocks inProviderRequest.assemble_context, where audio and video have separate branches(
entities.py:427-449and:456-478), and_prepare_conversationbuilds an inlinevideo part from a
video_urlblock (gemini_source.py:417). Video is deliverednatively.
The legacy shape —
text_chat(prompt=…), which is what plugin-facing helpers such asContext.llm_generateuse — builds the user message ingemini_source.py:1146(
assemble_context) instead. Its audio slot is the only media channel some callersexpose, so a video can legitimately arrive there, either pre-encoded as
data:video/mp4;base64,…or as a saved.mp4path.resolve_audio_partsent everysuch reference through
resolve_media_ref_to_base64_data(…, media_type="audio"), whichlands in the audio branch of
MediaResolver._resolve_path(media_utils.py:772) andre-encodes the payload to WAV.
Measured on generated mp4 files, before this change:
assemble_context(audio_urls=[…])audio_urlblock carryingaudio/wav, 88278 B — only the soundtrack reaches the model, the picture is goneThe video channel already exists on this path (
resolve_video_partatgemini_source.py:1201produces avideo_urlblock, and_prepare_conversationturns it into an inline part at
:417); it was simply never consulted for an audioreference. That is a classification bug, not a missing feature.
Changes
astrbot/core/utils/media_utils.pyVIDEO_MEDIA_SUFFIXES(:113) — the containers that carry video.looks_like_video_media_ref(media_ref)(:408) — true for adata:URI whosedeclared MIME type is
video/*, and for a file URI, an http(s) URL, or a local pathwhose path part ends in one of those containers.
base64://payloads andreferences that declare neither signal return
False, so they keep their currentbehaviour.
astrbot/core/provider/sources/gemini_source.pyTwelve added lines in total: one import and the branch inside
resolve_audio_part.Both call sites of the audio slot go through it — the
AudioURLPartbranch(
gemini_source.py:1250) and theaudio_urlsloop (:1271).After the change the same 12435 B file reaches the model as an inline
video/mp4part with its original bytes (12435 B) instead of 88278 B of WAV, and a video without
an audio track no longer disappears.
A video that cannot be resolved (unreadable payload, dead path) now leaves a
[Video]text block instead of nothing. That case used to be indistinguishable from"the message had no attachment at all".
Why
wrong step was the decision made before it, so the fix sits exactly there and the
conversion of genuine audio keeps its code.
this path, and returning a
video_urlblock costs nothing beyond the code that wasalready there. The placeholder is kept only for the failure case, where it is the
honest answer.
modalitiesbecause the assistant collects attachments on its own; on this path acaller explicitly attached the media (it asked for the audio slot in the first
place), so what this change corrects is the media family, not the capability list.
A follow-up change in the same series adds the modality gate for this path.
the runner, and
ProviderRequestare unchanged: modifying them would move behaviourfor every platform adapter and every provider in order to fix one provider's legacy
branch.
Verification
Diff scope: Own delta on top of
#10202is 2 production files (+74/-0):astrbot/core/utils/media_utils.py(+62/-0) andastrbot/core/provider/sources/gemini_source.py(+12/-0); full diff vs.masteris 20 files (+406/-18, no test files modified in this PR).Verified locally with 7 tests / 18 cases (standalone test file omitted from this PR diff; happy to include compact tests if preferred), no ffmpeg and no network:
data:video/mp4reference inaudio_urlsreaches_prepare_conversationas aninline_datapart withmime_type == "video/mp4"and the original bytes;AudioURLPartwhose URL is a video takes the same route;.mp4path calls the resolver withmedia_type="video"and never with"audio";{"type": "text", "text": "[Video]"};.wavanddata:audio/wavreferences still resolve withmedia_type="audio"and produce the sameaudio_urlblock;string, file URIs,
.m4a,base64://, empty input,None).Gemini and media regression surface (existing upstream suites — 169 passed; plus all 18 local classification cases and 23 local
#10202modality cases passed):python -m pytest tests/test_gemini_source.py tests/test_media_utils.py \ tests/test_agent_runner_media_resolver.py \ tests/test_model_image_preparation.py -q -n 4 -p no:cacheproviderCompatibility
media_type="audio", same WAV output,same
audio_urlblock.it is always delivered inline; the follow-up change in the same series makes that
delivery conditional on the provider declaring the
videomodality, which is therule every other route already applies. The locally detectable failure (an
unresolvable payload) degrades to
[Video]in both cases.text_chat,text_chat_stream, andassemble_contextkeep their signatures; no dashboard, i18n, or generated asset istouched.
their current behaviour, so callers that pass audio continue to get audio.
Privacy and delivery
path extension; it issues no request, runs no
ffprobe, and opens no file, so areference that is not a video costs nothing extra.
delivered, and it travels on the provider request that already carries audio.
Errors report a reference through
describe_media_ref, which prints the file nameand its length, not the full path; the
[Video]placeholder carries no path at all.captured media.
media_utils.py,gemini_source.py,+74/-0), with nogenerated artifact and no configuration change; nothing changes for a provider whose
callers do not put a video in the audio slot.
Suggested merge order
This branch is stacked on the native video input modality change and depends on it in
two ways: the
video_urlblock it produces is turned into an inline part by_prepare_conversation, which that change adds, and the same work is what makes videoa first-class attachment on the main path.
VideoURLPart, thevideomodality,the
video_urlstransport, andprocess_video_urlchannel
[Video]when the provider does not declare
videoBase. Open this pull request against the native video input modality branch, not
against
master. Against that base it shows this change alone — 2 files,+74 / -0(astrbot/core/utils/media_utils.py+62/-0,astrbot/core/provider/sources/gemini_source.py+12/-0) — because the branch is a direct descendant of#10202. Once the first changelands, retarget the pull request to
master; the diff does not change.Landing the first change is a precondition; this one can then be reviewed as a small,
self-contained classification fix. The OpenAI and Anthropic sources have their own
legacy
audio_urlsbranches (openai_source.py:1438,anthropic_source.py:1051)that likewise do not recognise a video; they are intentionally out of scope here, and
looks_like_video_media_reflives inmedia_utilsso they can reuse it later.Summary by Sourcery
Deliver video attachments as first-class, modality-gated inputs while preventing legacy audio paths from silently stripping their visual content.
New Features:
Bug Fixes:
[Video]placeholder while preserving existing audio behavior.Enhancements:
Tests: