Skip to content

feat: audio text to text support - #1396

Merged
markstur merged 13 commits into
generative-computing:mainfrom
markstur:feat/audio-text-to-text
Jul 17, 2026
Merged

markstur merged 13 commits into
generative-computing:mainfrom
markstur:feat/audio-text-to-text

Conversation

@markstur

Copy link
Copy Markdown
Contributor

Pull Request

Issue

Fixes #1394

Description

Adds support for audio similar to images in chat and instruct.

Testing

  • Tests added to the respective file if code was changed
  • New code has 100% coverage if code was added
  • Ensure existing tests and github automation passes (a maintainer will kick off the github automation when the rest of the PR is populated)

Attribution

  • AI coding assistants used

Adding a new component, requirement, sampling strategy, or tool?

If your PR adds or modifies one of the types below, check the matching box. A checklist of type-specific review items will be posted as a comment.

  • Component
  • Requirement
  • Sampling Strategy
  • Tool

NOTE: Please ensure you have an issue that has been acknowledged by a core contributor and routed you to open a pull request against this repository. Otherwise, please open an issue before continuing with this pull request.

@markstur
markstur requested a review from a team as a code owner July 15, 2026 06:57
@github-actions

github-actions Bot commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

This comment is managed by a bot. Editing it is fine — checking off boxes, adding notes — but please leave the HTML comment marker on the first line alone, otherwise checklist updates will break.

Component PR Checklist

Use this checklist when adding or modifying components in mellea/stdlib/components/.

Protocol Compliance

  • parts() returns list of constituent parts (Components or CBlocks)
  • format_for_llm() returns TemplateRepresentation or string
  • _parse(computed: ModelOutputThunk) parses model output correctly into the specified Component return type

Content Blocks

  • CBlock used appropriately for text content
  • ImageBlock used for image content (if applicable)

Integration

  • Component exported in mellea/stdlib/components/__init__.py or, if you are adding a library of components, from your sub-module

@github-actions github-actions Bot added the enhancement New feature or request label Jul 15, 2026

@ajbozarth ajbozarth left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some feedback from Claude — nicely done overall, faithful to the image pattern and well-tested. One correctness item on the MP3 path (inline) I'd like addressed before merge; the rest are questions and nits.

Comment thread mellea/core/base.py Resolved
Comment thread test/core/test_base.py Resolved
Comment thread mellea/core/base.py Resolved
Comment thread test/backends/test_audio_openai.py Resolved
markstur added 8 commits July 15, 2026 15:53
Introduces AudioBlock (base64 audio with auto-detected format from data URIs), AudioUrlBlock (http/https audio URL), and get_audio_from_component helper. Exports all three from mellea/core/__init__.py.

This is the core change for coming OpenAI API multimodal support for chat with audio
similar to chat with images.

Signed-off-by: Mark Sturdevant <[email protected]>
Add wiring for chat with audio similar to images.

Signed-off-by: Mark Sturdevant <[email protected]>
Assisted-by: IBM Bob
Add wiring for Instruction with audio similar to images.

Signed-off-by: Mark Sturdevant <[email protected]>
…nctional

* audio parameter implementation similar to images
* no special parsing like PIL handling is applicable

Signed-off-by: Mark Sturdevant <[email protected]>
* Relies on llama-server with gemma for local audio-text-to-text support to
  test with OpenAI API audio input.

Signed-off-by: Mark Sturdevant <[email protected]>
When the format is extracted from the data prefix, we get a MIME subtype.
But when we use the format, we need it to be a valid OpenAI API format.
There is a mismatch, but we can do some mapping.

mpeg -> mp3 might be the only standard mapping needed, but there are some other non-standard
or legacy mime subtypes that we also map now.

When no mapping is needed or it is not known, we just use the subtype and pass it on.
So the simple case like "wav" just works. Unsupported formats, or future things to map,
will also pass through and should produce a reasonable error for invalid format (400).

Signed-off-by: Mark Sturdevant <[email protected]>
Assisted-by: IBM Bob
…because standards

Signed-off-by: Mark Sturdevant <[email protected]>
Assisted-by: IBM Bob
@markstur
markstur force-pushed the feat/audio-text-to-text branch from 0624a11 to 9aae264 Compare July 15, 2026 23:06
… Ollama and Watsonx

Add explicit guards so AudioBlock/AudioUrlBlock passed to OllamaModelBackend
and any multimodal block passed to WatsonxAIBackend raise ValueError instead
of silently dropping the data. Add mocked unit tests for the new guards and
for OpenAI audio payload shape.

Signed-off-by: Mark Sturdevant <[email protected]>
Assisted-by: IBM Bob

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also update docs/examples/README.md to include these?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

Comment thread mellea/core/base.py Outdated
mime = value.split(";")[0].split("data:")[1] # e.g. "audio/wav"
subtype = mime.split("/")[-1] # e.g. "wav"
# Normalise non-standard / legacy MIME subtypes to OpenAI format tokens.
# OpenAI Chat Completions input_audio.format accepts: wav, mp3, ogg, flac.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's only wav and mp3

Image

(source)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think I should fix the comment (I somehow have a ruff fix todo anyway).

for openai that is correct, but open source servers (not openai cloud) tend to accept whatever their decoder libraries accept. So flac would be a good example of that. So other than the comment, I'm leaning into passing through because 1 - easier maintenance because the openai type support will change and 2 - there is evidence that vllm, for example, supports different formats.

Speculative mapping is questionable, but the initial map is based on common legacy mime types and common supported formats. So I don't see a need to make it just { mpeg -> mp3} but open to opinions on that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated comment so it is more accurate about the supported openai 2.

Also clarified a bit about the pass through of others and the mapping because that seemed a little contrary to the wording in the comments otherwise.

@ajbozarth ajbozarth left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — all my prior review points are addressed (MIME normalization, silent-drop guards on Ollama/Watsonx, RST cleanup). Approving.

Two inline nits below echo Paul's open points.

Non-blocking scope note (couldn't anchor inline — file isn't in this diff): the new Ollama/Watsonx guards close the silent-drop gap, but the HF chat path (_generate_from_context → to_chat → apply_chat_template) still stringifies messages and drops a stray audio= silently, same as it already does for images=. Pre-existing image behavior, not a regression from this PR, so fine to leave — fast-follow issue if anyone wants parity. (LiteLLM is unaffected; it serializes audio via message_to_openai_message.)

Comment thread mellea/core/base.py Outdated
mime = value.split(";")[0].split("data:")[1] # e.g. "audio/wav"
subtype = mime.split("/")[-1] # e.g. "wav"
# Normalise non-standard / legacy MIME subtypes to OpenAI format tokens.
# OpenAI Chat Completions input_audio.format accepts: wav, mp3, ogg, flac.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (+1 to @psschwei): the comment lists wav, mp3, ogg, flac, but the OpenAI input_audio schema only accepts wav and mp3. Code is still correct (unknown subtypes pass through to a 400), but worth trimming the comment to wav, mp3 — and note x-flac→flac normalizes to a value OpenAI will reject anyway.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed comment

nit: OpenAI the company/cloud will reject, but flac will work for servers/models that support it

@@ -0,0 +1,71 @@
# pytest: openai, e2e, qualitative

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit (+1 to @psschwei): add these examples to docs/examples/README.md so they're discoverable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@markstur
markstur added this pull request to the merge queue Jul 17, 2026
Merged via the queue into generative-computing:main with commit 019bb64 Jul 17, 2026
9 checks passed
@markstur
markstur deleted the feat/audio-text-to-text branch July 17, 2026 00:44
psschwei pushed a commit to psschwei/mellea that referenced this pull request Jul 17, 2026
* feat: add AudioBlock and AudioUrlBlock

Introduces AudioBlock (base64 audio with auto-detected format from data URIs), AudioUrlBlock (http/https audio URL), and get_audio_from_component helper. Exports all three from mellea/core/__init__.py.

This is the core change for coming OpenAI API multimodal support for chat with audio
similar to chat with images.

Signed-off-by: Mark Sturdevant <[email protected]>

* feat: add audio to Message and chat

Add wiring for chat with audio similar to images.

Signed-off-by: Mark Sturdevant <[email protected]>
Assisted-by: IBM Bob

* feat: add audio to instruction

Add wiring for Instruction with audio similar to images.

Signed-off-by: Mark Sturdevant <[email protected]>

* feat: add support for audio parts in openai_compatible_helpers

Signed-off-by: Mark Sturdevant <[email protected]>

* feat: add audio support to chat and instruct in stdlib session and functional

* audio parameter implementation similar to images
* no special parsing like PIL handling is applicable

Signed-off-by: Mark Sturdevant <[email protected]>

* feat: add examples and e2e test for audio-text-models

* Relies on llama-server with gemma for local audio-text-to-text support to
  test with OpenAI API audio input.

Signed-off-by: Mark Sturdevant <[email protected]>

* fix: mime subtypes for audio need to map to valid openai api formats

When the format is extracted from the data prefix, we get a MIME subtype.
But when we use the format, we need it to be a valid OpenAI API format.
There is a mismatch, but we can do some mapping.

mpeg -> mp3 might be the only standard mapping needed, but there are some other non-standard
or legacy mime subtypes that we also map now.

When no mapping is needed or it is not known, we just use the subtype and pass it on.
So the simple case like "wav" just works. Unsupported formats, or future things to map,
will also pass through and should produce a reasonable error for invalid format (400).

Signed-off-by: Mark Sturdevant <[email protected]>
Assisted-by: IBM Bob

* fix: replaced RST blocks and fixed dedenting in test_audio_openai.py because standards

Signed-off-by: Mark Sturdevant <[email protected]>
Assisted-by: IBM Bob

* fix(backends): raise ValueError for unsupported audio/image inputs in Ollama and Watsonx

Add explicit guards so AudioBlock/AudioUrlBlock passed to OllamaModelBackend
and any multimodal block passed to WatsonxAIBackend raise ValueError instead
of silently dropping the data. Add mocked unit tests for the new guards and
for OpenAI audio payload shape.

Signed-off-by: Mark Sturdevant <[email protected]>
Assisted-by: IBM Bob

* docs: add examples to docs/examples/README.md

Signed-off-by: Mark Sturdevant <[email protected]>

* fix: improve comment about supported audio input

Signed-off-by: Mark Sturdevant <[email protected]>

* fix: ruff format fix

Signed-off-by: Mark Sturdevant <[email protected]>

* fix: fix missing raises in docstring

Signed-off-by: Mark Sturdevant <[email protected]>

---------

Signed-off-by: Mark Sturdevant <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: audio-text-to-text support in chat and instruct

3 participants