Skip to content

fix(gemini): deliver a video that arrives through the audio slot - #11018

Open
JosephTian876 wants to merge 2 commits into
AstrBotDevs:masterfrom
JosephTian876:fix/video-not-audio-in-legacy-path-k2
Open

JosephTian876 wants to merge 2 commits into
AstrBotDevs:masterfrom
JosephTian876:fix/video-not-audio-in-legacy-path-k2

Conversation

@JosephTian876

@JosephTian876 JosephTian876 commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

依赖:本 PR 建立在上游 #10202(原生视频输入模态)之后,请先合并 #10202。单独查看时会包含 #10202 的 4 个提交;#10202 合并后,本 PR 自动只剩自己的 1 个提交。

fix(gemini): deliver a video that arrives through the audio slot

Background

ProviderGoogleGenAI has two request shapes, and only one of them treats a video as
a video.

The agent runner calls text_chat(contexts=…, extra_user_content_parts=…) without a
prompt (tool_loop_agent_runner.py:529-539). That shape assembles its blocks in
ProviderRequest.assemble_context, where audio and video have separate branches
(entities.py:427-449 and :456-478), and _prepare_conversation builds an inline
video part from a video_url block (gemini_source.py:417). Video is delivered
natively.

The legacy shape — text_chat(prompt=…), which is what plugin-facing helpers such as
Context.llm_generate use — builds the user message in gemini_source.py:1146
(assemble_context) instead. Its audio slot is the only media channel some callers
expose, so a video can legitimately arrive there, either pre-encoded as
data:video/mp4;base64,… or as a saved .mp4 path. resolve_audio_part sent every
such reference through resolve_media_ref_to_base64_data(…, media_type="audio"), which
lands in the audio branch of MediaResolver._resolve_path (media_utils.py:772) and
re-encodes the payload to WAV.

Measured on generated mp4 files, before this change:

Reference given to assemble_context(audio_urls=[…]) Result
1 s h264 video with an aac track, 12435 B an audio_url block carrying audio/wav, 88278 B — only the soundtrack reaches the model, the picture is gone
1 s h264 video with no audio track, 2718 B the conversion fails, the reference is logged and skipped — the attachment disappears and the model is never told it existed

The video channel already exists on this path (resolve_video_part at
gemini_source.py:1201 produces a video_url block, and _prepare_conversation
turns it into an inline part at :417); it was simply never consulted for an audio
reference. That is a classification bug, not a missing feature.

Changes

astrbot/core/utils/media_utils.py

  • VIDEO_MEDIA_SUFFIXES (:113) — the containers that carry video.
  • looks_like_video_media_ref(media_ref) (:408) — true for a data: URI whose
    declared MIME type is video/*, and for a file URI, an http(s) URL, or a local path
    whose path part ends in one of those containers. base64:// payloads and
    references that declare neither signal return False, so they keep their current
    behaviour.

astrbot/core/provider/sources/gemini_source.py

         async def resolve_audio_part(audio_path: str) -> dict | None:
+            # A caller that can only attach media through the audio slot may hand
+            # over a video (`data:video/mp4;...` or a saved .mp4 path). Resolving
+            # it as audio re-encodes it to WAV, which keeps the soundtrack and
+            # drops the picture, so route it to the video channel this path
+            # already has: `_prepare_conversation` turns the `video_url` block
+            # into an inline data part with the video MIME type.
+            if looks_like_video_media_ref(audio_path):
+                video_part = await resolve_video_part(audio_path)
+                # Reporting nothing would reproduce the silent loss this branch
+                # exists to prevent, so leave a placeholder instead.
+                return video_part or {"type": "text", "text": "[Video]"}
             try:
                 audio_data = await resolve_media_ref_to_base64_data(
                     audio_path,
                     media_type="audio",

Twelve added lines in total: one import and the branch inside resolve_audio_part.
Both call sites of the audio slot go through it — the AudioURLPart branch
(gemini_source.py:1250) and the audio_urls loop (:1271).

After the change the same 12435 B file reaches the model as an inline video/mp4
part 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

  • Only the classification is fixed. The WAV conversion is correct for audio; the
    wrong step was the decision made before it, so the fix sits exactly there and the
    conversion of genuine audio keeps its code.
  • Routed to the video channel rather than degraded. The channel is reachable on
    this path, and returning a video_url block costs nothing beyond the code that was
    already there. The placeholder is kept only for the failure case, where it is the
    honest answer.
  • No capability list in this change. On the runner path video is opt-in through
    modalities because the assistant collects attachments on its own; on this path a
    caller 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.
  • Everything else is untouched. The media resolver, the outbound audio senders,
    the runner, and ProviderRequest are unchanged: modifying them would move behaviour
    for every platform adapter and every provider in order to fix one provider's legacy
    branch.

Verification

Diff scope: Own delta on top of #10202 is 2 production files (+74/-0): astrbot/core/utils/media_utils.py (+62/-0) and astrbot/core/provider/sources/gemini_source.py (+12/-0); full diff vs. master is 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:

  • a data:video/mp4 reference in audio_urls reaches _prepare_conversation as an
    inline_data part with mime_type == "video/mp4" and the original bytes;
  • an AudioURLPart whose URL is a video takes the same route;
  • a saved .mp4 path calls the resolver with media_type="video" and never with
    "audio";
  • an unresolvable video leaves {"type": "text", "text": "[Video]"};
  • regression: .wav and data:audio/wav references still resolve with
    media_type="audio" and produce the same audio_url block;
  • classifier positives and negatives (upper-case extensions, signed URLs with a query
    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 #10202 modality 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:cacheprovider
ruff format --check .   # clean
ruff check .            # All checks passed!

Compatibility

  • Audio is unchanged. Same resolver, same media_type="audio", same WAV output,
    same audio_url block.
  • A reference that is recognisably a video no longer arrives as WAV. On this change
    it is always delivered inline; the follow-up change in the same series makes that
    delivery conditional on the provider declaring the video modality, which is the
    rule every other route already applies. The locally detectable failure (an
    unresolvable payload) degrades to [Video] in both cases.
  • No public interface change. text_chat, text_chat_stream, and
    assemble_context keep their signatures; no dashboard, i18n, or generated asset is
    touched.
  • References that declare neither a video MIME type nor a known container keep
    their current behaviour
    , so callers that pass audio continue to get audio.

Privacy and delivery

  • Classification is local and cheap. The check reads a declared MIME type or a
    path extension; it issues no request, runs no ffprobe, and opens no file, so a
    reference that is not a video costs nothing extra.
  • No new egress and no new logging. The media is read only when it is about to be
    delivered, and it travels on the provider request that already carries audio.
    Errors report a reference through describe_media_ref, which prints the file name
    and its length, not the full path; the [Video] placeholder carries no path at all.
  • No credentials or user data in the diff. The change adds no token, key, or
    captured media.
  • Delivery. Own delta touches two production source files (media_utils.py, gemini_source.py, +74/-0), with no
    generated 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_url block it produces is turned into an inline part by
_prepare_conversation, which that change adds, and the same work is what makes video
a first-class attachment on the main path.

  1. the native video input modality change — adds VideoURLPart, the video modality,
    the video_urls transport, and process_video_url
  2. this change — recognises a video in the legacy audio slot and routes it to that
    channel
  3. the follow-up modality gate for this path — degrades the injection to [Video]
    when the provider does not declare video

Base. 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 change
lands, 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_urls branches (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_ref lives in media_utils so 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:

  • Add end-to-end video input support across agent requests, provider contexts, Gemini processing, and provider capability metadata.
  • Expose video capability badges and configuration support in the dashboard.

Bug Fixes:

  • Preserve videos supplied through legacy audio attachment paths instead of converting them to audio and losing the visual track.
  • Degrade unsupported or unresolvable video content to an explicit [Video] placeholder while preserving existing audio behavior.

Enhancements:

  • Add video-aware modality filtering, context sanitization, transient history handling, and media-reference classification.

Tests:

  • Add coverage for video routing, modality handling, media classification, failure placeholders, and audio regressions.

@sourcery-ai

sourcery-ai Bot commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

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 @sourcery-ai review. Upgrade to get a review now.

@codecov-commenter

codecov-commenter commented Nov 5, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 76.97368% with 35 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
astrbot/core/provider/entities.py 64.10% 14 Missing ⚠️
astrbot/core/provider/sources/gemini_source.py 58.82% 14 Missing ⚠️
astrbot/core/provider/modalities.py 92.30% 3 Missing ⚠️
astrbot/core/provider/sources/anthropic_source.py 33.33% 2 Missing ⚠️
astrbot/core/provider/sources/openai_source.py 0.00% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@Soulter Soulter moved this to In Progress in AstrBot Roadmap Nov 6, 2026
@JosephTian876
JosephTian876 force-pushed the fix/video-not-audio-in-legacy-path-k2 branch from f3d8ea7 to 6b5b91c Compare November 21, 2026 21:36
@JosephTian876

Copy link
Copy Markdown
Contributor Author

Rebased onto updated #10202 (2c3e5dbdb, atop master feb3e82c3) with no changes to this PR's scoped diff (gemini_source.py, media_utils.py, test_gemini_legacy_media_kind.py). Verified with pytest and ruff check/ruff format --check.

@JosephTian876

Copy link
Copy Markdown
Contributor Author

Rebased onto the updated #10202 branch (a084409 base) and trimmed newly added test files per .github/copilot-instructions.md to keep the diff focused on production changes.

Local Verification

  • Pre-trim regression tests: 18 passed (test_gemini_legacy_media_kind.py)
  • Existing upstream test suite: 105 passed (test_gemini_source.py, test_media_utils.py)
  • ruff check and ruff format --check: passed (0 issues)

建议合并顺序

  1. #10894(修复历史图片无法下载导致对话失败,改动最小,优先合入)
  2. #10202 → #11018 → #11019(原生视频输入模态与遗留路径视频槽位修复,已依次堆叠)
  3. #11298 → #11185(Gemini 中转站 usageMetadata 回填与流式工具调用批次/ID 保留,均改动 _query_stream,按序合入后次序者轻微对齐即可)

@JosephTian876
JosephTian876 force-pushed the fix/video-not-audio-in-legacy-path-k2 branch from 8db94be to 7b5d2d7 Compare December 1, 2026 20:29

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

2 participants