Skip to content

fix: Gemma 4 server audio (input_audio) places <|audio|> outside the user turn (0-token output) #437

Closed

Description

@inureyes

Summary

Follow-up to #436 (CLI Gemma 4 audio). The server (mlxcel-server / /v1/chat/completions) Gemma 4 audio path still places the <|audio|> block outside the user turn, the same placement defect #436 fixed for the CLI. #436 fixed the server resampling (Fix B, in prepare_gemma4_audio_embeddings) but deliberately deferred the server placement fix (Fix A) because it requires reworking the prefix-cache-sensitive prompt rendering path, which is out of scope for that bug fix.

Root cause

The server renders chat messages text-only. build_chat_messages_with_thinking / build_raw_json_messages_with_thinking (src/server/chat_request.rs) flatten each message to m.content.text() and never emit {"type":"audio"} (or image/video) content parts. So for an input_audio request the chat template never renders an <|audio|> marker into the user turn.

expand_gemma4_audio_tokens_for_server (src/multimodal/vlm_runtime.rs) then finds no placeholder and uses its "insert before the last token" fallback, which for the Gemma generation prompt (<bos><start_of_turn>user\n{text}<end_of_turn>\n<start_of_turn>model\n) lands the BOA + audio*N + EOA block inside the model turn. As shown empirically in #436, audio in the model turn forces an immediate EOS, so the server emits 0 tokens for Gemma 4 audio understanding.

The OpenAI content type on the wire is input_audio (src/server/types/request.rs), while the gemma-4 template expects a content item of type audio (chat_template.jinja: {%- elif item['type'] == 'audio' -%} {{- '<|audio|>' -}}), so even a multimodal rendering path must map input_audio to an audio content item.

Evidence (from #436)

  • 24 kHz input on the CLI before the fix: encoder output [1, 64, 1536] vs 43 placeholders -> 0 tokens, even with the audio token correctly placed (that was the resample defect, now fixed on both CLI and server).
  • Audio placed in the model turn (no <|audio|> in the user turn) -> immediate EOS. BOA@index 17 while the two <start_of_turn> markers are at [1, 11]; the audio block (17..61) sits after the model-turn marker.
  • The CLI fix (fix: Gemma 4 audio understanding (CLI --audio) produces no output (missing 16kHz resample + audio token placed outside the user turn) #436) renders {"type":"audio"} into the user turn so the template emits <|audio|> and expand_gemma4_audio_tokens expands it in place. The server needs the analogous rendering.

Proposed fix

Render an audio content part into the user turn for server requests that carry input_audio, so the gemma-4 template emits <|audio|> and expand_gemma4_audio_tokens_for_server expands it in place instead of using the model-turn fallback. Options:

  1. Extend the server multimodal rendering to emit {"type":"audio"} (and, for symmetry, image/video) content parts into the user turn when the model's template supports them (ChatTemplateProcessor::supports_audio_content() already exists after fix: Gemma 4 audio understanding (CLI --audio) produces no output (missing 16kHz resample + audio token placed outside the user turn) #436), mirroring the CLI apply_vlm_chat_template. Must preserve prompt-cache prefix stability (see the src/server/chat_request.rs module docs) and stay byte-identical for non-audio requests.
  2. Or, as a narrower interim fix, change expand_gemma4_audio_tokens_for_server to insert before the final <end_of_turn> of the last user turn rather than before the generation prompt (requires threading the end-of-turn token id through).

Acceptance criteria

Notes

The server resampling half of #436 (Fix B) is already merged in the #436 PR; this issue tracks only the server placement (Fix A) half.

Activity

  1. added
    status:ready
    type:bug
    priority:medium
    area:inference
    status:in-progress
    status:review
    and removed
    status:ready
    status:in-progress
    on Jun 25, 2026
  2. self-assigned this
    on Jun 25, 2026
  3. added
    status:done
    and removed
    status:review
    on Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:inferencepriority:mediumstatus:donetype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions