Skip to content

Studio: support external OpenAI-compatible TTS endpoints for Read Aloud - #9214

Merged
Etherll merged 19 commits into
unslothai:mainfrom
NilayYadav:studio-external-tts
Aug 24, 2026
Merged

Etherll merged 19 commits into
unslothai:mainfrom
NilayYadav:studio-external-tts

Conversation

@NilayYadav

Copy link
Copy Markdown
Collaborator

Adds a "Custom endpoint" TTS engine in Settings → Voice. Read Aloud can now use a saved connection's OpenAI-compatible /v1/audio/speech endpoint (e.g. Kokoro) instead of loading a local TTS model, keeping VRAM free for the main model. Endpoint failures show a clear error instead of falling back to a local model load.

Tested end-to-end against a local Kokoro server. Closes #9136.

@danielhanchen

danielhanchen commented Aug 19, 2026 •

Copy link
Copy Markdown
Member

Confirmed this adds the external /audio/speech proxy behind the new optional provider_id on AudioSpeechRequest, leaving the local WAV path untouched, and validating the base URL before the key is decrypted is the right order.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

# Conflicts:
#	studio/backend/core/inference/external_provider.py
#	studio/frontend/src/features/settings/tabs/voice-tab.tsx
NilayYadav pushed a commit to NilayYadav/unsloth-staging that referenced this pull request Aug 21, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c596eb2548

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread studio/backend/routes/inference.py Outdated
async def _external_tts_speech(body: AudioSpeechRequest, request: Request) -> Response:
"""Proxy CreateSpeech to a saved connection's /audio/speech, so read-aloud can use a
remote TTS server (e.g. Kokoro) without occupying the local model slot."""
config = providers_db.get_provider(body.provider_id)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Move saved-provider reads off the event loop

When studio.db is locked, slow, or initializing its schema, this synchronous provider lookup—and the subsequent saved-key resolution—can block for up to SQLite's five-second timeout on FastAPI's event-loop thread, preventing unrelated requests and liveness checks from being served. _external_stt_transcription already performs the same operations with asyncio.to_thread while using provider_config_guard and a routing-field recheck to preserve an atomic target/key snapshot; the TTS path should follow that pattern as well.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Not taking this one. The synchronous read here is the codebase norm rather than this path's anomaly: providers_db.get_provider is called directly on the event loop at 11 sites, 10 of which this PR does not touch (routes/providers.py x7, research_runs.py, openai_codex_auth.py, and inference.py:14450). More to the point, inference.py:14450 documents the reasoning for doing it exactly this way: 'Callers run this on the event loop, not in a worker: the row read and the credential read have to see one provider, or an edit landing between them pairs the old base URL with the new key.' That is why the transcription path needs provider_config_guard, a routing-field recheck and a 409 on top of its to_thread hop; the hop is what creates the interleaving it then has to defend against. Adopting it here would trade a contention-only latency concern for that extra concurrency surface and a new failure mode, against a documented in-file invariant. No wrong data is sent, dropped or lost, and the trigger is a locked or slow database rather than anything in a real session, so this sits below the bar for changing code.

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c596eb2548

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

input: text,
provider_id: ttsProviderId,
model,
...(voice ? { voice } : {}),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Require a voice for OpenAI-compatible TTS

When a first-time user leaves the Voice field blank—as encouraged by the “optional” description and the non-binding alloy placeholder—this conditional omits voice entirely. OpenAI's Create Speech API requires voice, as do strict compatible servers, so a correctly configured OpenAI connection returns a validation error and Read Aloud fails. Require a non-empty voice or send a real default value rather than relying on the placeholder.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Checked the factual claim and it is right: OpenAI's own OpenAPI schema lists CreateSpeechRequest as required [model, input, voice], so a real OpenAI connection with the field blank does get rejected. Still declining, because both suggested remedies break the case this feature exists for. Sending a real default means sending alloy, which is not a Kokoro voice and would turn a working Kokoro setup into a failing one; requiring a non-empty voice does the same to every OpenAI-compatible server that picks its own default. Omitting the key when blank is what lets one field serve both, which is why the description says optional. The alternative is pre-validating per provider_type, and that only moves the error's origin: the user has already been made to type a model (guarded in both the adapter and the route), a blank voice against OpenAI comes back as an explicit missing-parameter message surfaced through the 502 path, and Read Aloud does not play either way. Same outcome, nicer wording, so it is below the bar for a code change. The placeholder reading as a default is a fair point and worth a copy tweak, not logic.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@NilayYadav

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4327c5941e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread studio/backend/routes/inference.py Outdated
Comment on lines +12122 to +12126
api_key = resolve_provider_api_key_or_400(
body.provider_id,
body.encrypted_api_key,
allow_saved_key = not _request_has_api_key(request),
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Snapshot the provider route and key atomically

When a connection's base URL and API key are edited while custom TTS is starting, this code can read the old provider configuration and then resolve the newly saved key, sending the new credential to the old endpoint. The update path writes routing metadata and the replacement key together under provider_config_guard (routes/providers.py:454-461), but this read path does not participate in that guard. Acquire the provider guard and re-read/recheck the routing fields before resolving the key, as _external_stt_transcription already does.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Checked this closely and the snapshot is already atomic here. provider_config_guard returns an asyncio.Lock, which only serializes coroutines at await points, and there is no await anywhere between the provider row read and the key resolution in this function: get_provider, get_base_url, validate_provider_base_url and resolve_provider_api_key_or_400 are all plain def, so the whole span runs as one uninterrupted synchronous block on the single threaded loop. The update handler is an async coroutine holding that same lock, so it cannot land in the middle. That is not incidental, it is the documented strategy: inference.py:14450 spells it out for the sibling read path, 'Callers run this on the event loop, not in a worker: the row read and the credential read have to see one provider, or an edit landing between them pairs the old base URL with the new key.' _external_stt_transcription needs the guard and the routing field recheck because it resolves the key through await asyncio.to_thread, and that hop is what opens the window it then has to defend against. Worth noting these last two suggestions pull against each other: the earlier one asked to move these same reads off the event loop with to_thread, which is precisely what would create the race described here. Keeping the reads inline is what makes the pairing safe.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 4327c5941e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: e9e889ecd6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 24, 2026
@unslothai unslothai deleted a comment from NilayYadav Aug 24, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 24, 2026
@unslothai unslothai deleted a comment from NilayYadav Aug 24, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 24, 2026
@unslothai unslothai deleted a comment from NilayYadav Aug 24, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 24, 2026
@unslothai unslothai deleted a comment from NilayYadav Aug 24, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 24, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 24, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Aug 24, 2026
@unslothai unslothai deleted a comment from NilayYadav Aug 24, 2026
@unslothai unslothai deleted a comment from NilayYadav Aug 24, 2026
@unslothai unslothai deleted a comment from NilayYadav Aug 24, 2026
@unslothai unslothai deleted a comment from NilayYadav Aug 24, 2026
@unslothai unslothai deleted a comment from NilayYadav Aug 24, 2026
@unslothai unslothai deleted a comment from NilayYadav Aug 24, 2026
@Etherll

Etherll commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

@codex review

1 similar comment
@Etherll

Etherll commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 56645f6045

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

"The custom TTS connection changed while the request was starting. Try again.",
);
}
const response = await authFetch("/api/inference/audio/speech", {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Recheck the connection switch before authenticated retries

When the access token has expired and the user switches off Connections while authFetch is refreshing it, this call can still release the assistant text to the custom endpoint: authFetch handles the initial 401 by awaiting refreshSession() and then calling retryWithCurrentToken() internally, so the guard at line 299 is never rerun before the successful retry. The new encryption-race check therefore does not uphold the UI's “all connections are disabled” policy in this common authentication path; use a retry-aware guard or otherwise revalidate the switch immediately before an authenticated retry.

Useful? React with 👍 / 👎.

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.

Fixed in a3ee192. authFetch now runs a synchronous policy guard immediately before every authenticated or network retry, and the custom TTS adapter rechecks the exact connection snapshot there. Added regressions for both the adapter path and the 401-refresh-retry path.

@Etherll

Etherll commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: a3ee19283f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Etherll Etherll 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.

LGTM!

@Etherll
Etherll merged commit a39638f into unslothai:main Aug 24, 2026
50 of 51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Support external/custom TTS endpoints for Read Aloud in Voice settings

3 participants