Migrating from Streaming v2 to Streaming v3 (Python)
Migrating from Streaming v2 to Streaming v3 (Python)
Migrating from Streaming v2 to Streaming v3 (Python)
This cookbook guides you through migrating from AssemblyAI’s legacy Streaming STT model (v2) to our latest Universal Streaming STT model (v3), which provides ultra-low latency for faster transcription, intelligent endpointing for more natural speech detection, and improved accuracy across various audio conditions.
Check out this blog post to learn more about this new model!
The migration involves several key improvements:
/v2/realtime/ws) to v3 (/v3/ws)You can follow the step-by-step guide below to make changes to your existing code but here is what your code should look like in the end:
For more information on our Universal Streaming feature, see this section of our official documentation.
Before (v2):
After (v3):
Key Changes:
streaming.assemblyai.com instead of api.assemblyai.com/v3/ws instead of /v2/realtime/wsurlencode()format_turns option for better transcript formattingBefore (v2):
After (v3):
Key Changes:
Before (v2):
After (v3):
Key Changes:
threading.Event() for controlled thread terminationaudio_thread variable for better lifecycle managementwhile not stop_event.is_set()) instead of infinite loopBefore (v2):
After (v3):
Key Changes:
SessionBegins → Begin, PartialTranscript/FinalTranscript → Turnmessage_type → type, session_id → id, text → transcriptturn_is_formatted flagTermination message handling with session statisticsJSONDecodeError catchBefore (v2):
After (v3):
Key Changes:
stop_event.set()on_errorBefore (v2):
After (v3):
Key Changes:
finally blockStreaming is billed on the total duration that your WebSocket connection stays open (session duration), not on the amount of audio sent. Always terminate sessions explicitly to avoid being billed for idle time — unterminated sessions auto-close after 3 hours and are billed for the full duration. See Streaming Speech-to-Text billing for details.
Before (v2):
After (v3):
streaming.assemblyai.comBegin, Turn, Termination)threading.Event() for thread controlformat_turns enabled/disabledSolution: Verify you’re using the new v3 endpoint URL and proper authentication header format.
Solution: Update message type handling from old names (SessionBegins, PartialTranscript) to new ones (Begin, Turn).
Solution: Ensure you’re using threading.Event() and calling stop_event.set() in error handlers.
Solution: Verify all audio resources are properly cleaned up in on_close and finally blocks.
This migration provides a more robust, maintainable, and feature-rich streaming transcription implementation. The enhanced error handling, resource management, and modern API features make it suitable for production use cases where reliability and performance are critical.