What a dialogue pipeline needs from the model
Dialogue for audio is a narrow job with strict requirements. Every line belongs to a named speaker. Lines are short enough to say in one breath. Stage directions must be separable from spoken words, because a voice tool will read aloud anything you hand it. And the characters must stay themselves across twenty scenes, not drift toward the same polite register.
The refusal problem is the other half. Scripts for adult audiences, such as crime drama, horror, war fiction, bitter family arguments or companion-style roleplay, contain material that mainstream assistants often soften or decline. When a model stops a scene to add a disclaimer, the line is unusable and you pay a retry. This API serves one open-weight model, run by an independent provider, tuned to answer without content refusals for lawful adult use. The hard limit is the same everywhere: no sexual content involving minors.
What it does not do is speak. It is text only. The output goes to whatever you use for voices, which could be a text-to-speech engine, a recording session with actors, or a game dialogue system. Everything below is about producing clean text for that next step.
Connect with any OpenAI client
The API follows the OpenAI chat format. Create a key on the API key page, then set the base URL to https://api.cosyvoice.top/v1 and the model to uncensored. Authentication is a standard Bearer header. Tools that let you add a custom OpenAI-compatible endpoint, such as SillyTavern or many desktop chat clients, need only those three values.
For scripted batches, use code. A first call to check that everything works:
curl https://api.cosyvoice.top/v1/chat/completions \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "uncensored",
"messages": [{"role": "user", "content": "Write four lines of tense dialogue between a night-shift nurse and a stranger in a corridor."}],
"temperature": 0.9
}'
Plain text is fine for exploring a scene. For production, move to structured output as shown next.
A system prompt that produces voice-ready lines
The system prompt does three things: it fixes the cast, it fixes the output shape, and it sets the rules for what a spoken line may contain. Here is a version that works as a starting point.
SYSTEM = """You are a dialogue writer for an audio drama for adults.
CAST
- MARA: ferryboat captain, 50s, dry humor, never raises her voice.
- DELL: her nephew, 20s, anxious, talks fast, avoids direct answers.
- OSTER: harbor inspector, formal, speaks in full sentences.
RULES
- Output JSON only: {"scene": "<short title>", "lines": [{"speaker": "MARA", "text": "...", "direction": "..."}]}
- "text" is exactly what the voice actor says. No stage directions, no markdown, no emoji.
- "direction" is a few words of delivery guidance, or an empty string.
- Each line is below 35 words. Spell out numbers and symbols.
- Stay in character. Do not add commentary, warnings or summaries outside the JSON.
"""
Splitting text from direction is the single most useful decision. It lets a downstream tool voice the first and use the second as a tag or a note to a performer, instead of reading brackets aloud.
Request JSON and parse it
The API supports JSON mode through response_format. JSON mode returns a JSON object, so the top level must be an object with a lines array, which is why the prompt above asks for one. The code below requests a scene and prints a clean read-through.
import json
from openai import OpenAI
client = OpenAI(base_url="https://api.cosyvoice.top/v1", api_key="YOUR_KEY")
def write_scene(brief: str, history: list[dict] | None = None) -> dict:
messages = [{"role": "system", "content": SYSTEM}]
messages += history or []
messages.append({"role": "user", "content": brief})
resp = client.chat.completions.create(
model="uncensored",
messages=messages,
response_format={"type": "json_object"},
temperature=0.85,
top_p=0.95,
)
return json.loads(resp.choices[0].message.content)
scene = write_scene("Dell confesses he lost the ferry's logbook. Oster arrives mid-conversation.")
for line in scene["lines"]:
print(f'{line["speaker"]}: {line["text"]}')
Wrap json.loads in a try block in real use. JSON mode keeps output well formed far more often than free text does, but any model can occasionally truncate or add a stray character, and a single retry costs a fraction of a cent. Failed or refused requests are not charged, which keeps retry loops cheap.
Keeping characters consistent across scenes
Continuity is where a 100,000-token window earns its place. The window counts the prompt and the completion together, so a long script project can carry a lot of history in every call. A practical layout:
- The system prompt holds the cast sheet and the format rules. It never changes between calls.
- A short "story so far" message summarizes earlier scenes in a few hundred words.
- The last one or two scenes appear verbatim, so the model hears the real rhythm of each character.
- The newest user message states what happens in this scene.
Resist the urge to paste the entire draft every time. A model given sixty thousand tokens of earlier script will copy its phrasing, and you pay for the extra input on every call. A summary plus the previous scene is usually enough, and the full window is there when you need a callback to something said much earlier.
Sampling settings matter too. Lower temperature (0.6 to 0.7) gives steadier, more predictable lines, which suits a character like Oster. Higher values (0.9 and up) produce livelier but looser speech. You can vary temperature per character by generating their lines in separate calls, though a single call per scene is easier to keep coherent. The seed parameter is passed through, which helps when you want to reproduce a take while changing one instruction. Penalty settings can reduce repeated catchphrases.
Writing lines that sound spoken
Text that reads well on a page can fall flat in a voice actor's mouth. A few instructions in the system prompt make a visible difference, and you can add them one at a time as you hear problems in a read-through.
- Ask for contractions and sentence fragments. People interrupt themselves, trail off and answer a question with a question.
- Give each character a verbal habit: Mara answers with a short dry remark, Dell starts sentences with "Okay, so" and circles back. One habit per character is enough, and the model will apply it consistently.
- Ban dense information in a single line. If a character has to explain a plan, split it across two speakers or add a reaction line between the facts.
- Ask for the exact spelling of anything unusual, such as a place name, so a voice tool pronounces it the same way each time.
Revising one line without redoing the scene
When one line is wrong, do not regenerate the scene. Send the scene JSON back as an assistant message, then a short user message such as "Rewrite line 7 so Dell sounds more evasive. Return the full JSON again." The model returns the whole object with only that line changed, you keep the rest of the take, and the call costs a few thousand input tokens, which is a small fraction of a cent. This also keeps recorded or already voiced lines stable, which matters once a human actor has read them.
Use tool calling for longer production runs
Once a script grows past a handful of scenes, bookkeeping becomes the problem: who knows which secret, which props have been used, which scenes are done. The API supports tool calling with tools and tool_choice, and tool results go back as messages with role: "tool". A simple pattern is to define a lookup_character function that returns the stored notes for a name, and let the model call it when a scene involves someone it needs details on. Your code runs the lookup and returns the result.
This keeps the system prompt short, which lowers input cost and leaves more of the window for scene text. It is optional. Many projects work fine with a static cast sheet, and the docs show the exact request fields if you want to try tools.
What a script run costs
Dialogue is cheap to generate. Take a scene with a 3,000-token prompt (cast sheet, summary and previous scene) and a 700-token JSON reply. Input is 3,000 x $0.15 / 1,000,000 = $0.00045. Output is 700 x $0.50 / 1,000,000 = $0.00035. The scene costs $0.0008. A 60-scene audio drama generated once is $0.048, and even if you regenerate every scene five times to pick the best take, the total is $0.24.
The rates are $0.15 per 1M input tokens and $0.50 per 1M output tokens, prepaid, with no subscription and no expiry on credit. Top-ups run from $20 to $500, with bonuses of 5%, 10% and 15% from $50, $100 and $200. New accounts can claim $0.50 of trial credit with one click after sign-up. See the pricing page for the full table and payment methods.
On limits: a full key allows 8 concurrent requests and 300 per minute, and a trial key allows 2 and 60. If you generate scenes in parallel, cap your workers at those numbers. A 429 with code concurrency means too many calls at once, and rate_limited means too many per minute. Back off and retry.
Before you voice it
Read the output aloud once before sending it to a voice tool. Awkward rhythm and tongue-twisters are easy to hear and hard to see, and one extra instruction in the system prompt usually fixes a recurring problem across the whole project.
