Skip to content

fix: preserve Gemini answer text in speech engine streams - #929

Merged
PaulAsjes merged 1 commit into
elevenlabs:mainfrom
vinsymoyya:fix/speech-engine-gemini-text
Nov 10, 2026
Merged

PaulAsjes merged 1 commit into
elevenlabs:mainfrom
vinsymoyya:fix/speech-engine-gemini-text

Conversation

@vinsymoyya

@vinsymoyya vinsymoyya commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #928.

Speech Engine currently takes only the first part of a Gemini response chunk. A reply split into "Hello" and " world" is spoken as "Hello"; a leading non-text part drops the answer, while a leading thought part is spoken instead of the answer.

The extractor now concatenates all string-valued text parts of the first candidate and skips parts marked thought=True. This matches Google's response text extraction. Other provider formats and the first-candidate selection remain unchanged.

The change touches only the hand-maintained Speech Engine session and its existing test file, both covered by .fernignore. The new tests exercise send_response() with dictionary and attribute-style chunks, including mixed parts, thought-only responses, empty text, candidate selection, event IDs and final-response signaling. They are distinct from #878's helper characterization tests.

Validation on Python 3.11:

  • New regression cases before the fix: 10 failed, 2 passed. After: 12 passed.
  • Speech Engine suites: 76 passed. Broader offline SDK suite: 238 passed.
  • mypy src/elevenlabs/speech_engine/session.py tests/test_speech_engine_session.py: passed.
  • Ruff on the test file and source checks excluding the unchanged baseline F401 import: passed.
  • Wheel build and git diff --check: passed.

This uses response fixtures, without live Gemini or ElevenLabs API calls.

Hosted validation of exact commit 6b806692300b0079adce3652dc9f910638c1a3e7: fork offline CI passed on Python 3.8, 3.13 and 3.14 (238 tests per version), including test lint, a Python 3.8 type check and a Python 3.13 wheel build. The workflow lives only on a separate fork validation branch, outside this PR.

The native Python 3.14 job passes 357 tests and fails 16 live API tests with HTTP 401 because the fork lacks an API credential. The unchanged upstream baseline fails the identical 16 tests. The native suite is therefore not reported as green.

@cursor

cursor Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Localized to Gemini stream parsing in the hand-maintained speech engine module, with broad test coverage and no auth or data-path changes.

Overview
Fixes incorrect TTS when streaming Google Gemini replies through Speech Engine: the session no longer reads only the first parts[0].text entry from the first candidate.

_extract_text now walks every part on candidates[0].content.parts, drops parts with thought=True, collects string text values (including after non-text parts like function calls), and joins them into one spoken string. Other providers and “first candidate only” behavior are unchanged.

Regression tests drive send_response() with dict- and attribute-style Gemini chunks, covering split text, mixed parts, thought-only replies, empty text, and final/event_id signaling.

Reviewed by Cursor Bugbot for commit 6b80669. Bugbot is set up for automated code reviews on this repo. Configure here.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the change preserving multipart Gemini answer text while excluding thought content.

No open issues 🎉

What changed in this PR

Fixes #928 by preserving multipart Gemini answer text in Speech Engine streams while excluding thought content. Other provider formats and first-candidate selection remain unchanged.

Changes:

  • Concatenates all string-valued, non-thought text parts from the first Gemini candidate.
  • Adds dictionary- and attribute-style regression tests covering mixed parts, empty responses, candidate selection, event IDs, and final-response signaling.
File Description
tests/​test_speech_engine_session.py Adds parameterized Gemini stream tests through send_response().
src/​elevenlabs/​speech_engine/​session.py Collects Gemini answer text across parts, skips thoughts, and updates the extractor docstring.

Review effort: Balanced

@PaulAsjes PaulAsjes left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks!

@PaulAsjes
PaulAsjes merged commit a204be9 into elevenlabs:main Nov 10, 2026
1 check passed
PaulAsjes added a commit to elevenlabs/elevenlabs-js that referenced this pull request Nov 11, 2026
Join all text parts of the first candidate instead of reading only
parts[0], and skip thought parts so model reasoning isn't sent to TTS.
Mirrors elevenlabs/elevenlabs-python#929.

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Speech Engine truncates multipart Gemini answers and includes thought text

3 participants