{"templateId":"markdown","sharedDataIds":{"sidebar":"sidebar-sidebars.yaml"},"props":{"metadata":{"markdoc":{"tagList":["admonition","partial"]},"type":"markdown"},"seo":{"title":"Python Code Samples","llmstxt":{"hide":false,"sections":[{"title":"Table of contents","includeFiles":["**/*"],"excludeFiles":[]}],"excludeFiles":[]}},"dynamicMarkdocComponents":[],"compilationErrors":[],"ast":{"$$mdtype":"Tag","name":"article","attributes":{},"children":[{"$$mdtype":"Tag","name":"Heading","attributes":{"level":1,"id":"python-code-samples","__idx":0},"children":["Python Code Samples"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Use the Python code samples below to quickly get started developing with the Rev AI APIs."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"submit-a-local-file-for-transcription","__idx":1},"children":["Submit a local file for transcription"]},{"$$mdtype":"Tag","name":"Admonition","attributes":{"type":"info"},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["This example uses the ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"/sdk/python"},"children":["Rev AI Python SDK"]},"."]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The following example demonstrates how to submit a local audio file for transcription."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["To use this example, replace the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["<FILEPATH>"]}," placeholder with the path to the file you wish to transcribe and the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["<REVAI_ACCESS_TOKEN>"]}," placeholder with your Rev AI account's access token."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"python","header":{"controls":{"copy":{}}},"source":"from rev_ai import apiclient\n\ntoken = \"<REVAI_ACCESS_TOKEN>\"\nfilePath = \"<FILEPATH>\"\n\n# create your client\nclient = apiclient.RevAiAPIClient(token)\n\n# send a local file\njob = client.submit_job_local_file(filePath)\n\n# check job status\njob_details = client.get_job_details(job.id)\n\n# retrieve transcript as text\ntranscript_text = client.get_transcript_text(job.id)\n\n# retrieve transcript as JSON\ntranscript_json = client.get_transcript_json(job.id)\n\n# retrieve transcript as a Python object\ntranscript_object = client.get_transcript_object(job.id)\n","lang":"python"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"submit-a-remote-file-for-transcription","__idx":2},"children":["Submit a remote file for transcription"]},{"$$mdtype":"Tag","name":"Admonition","attributes":{"type":"info"},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["This example uses the ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"/sdk/python"},"children":["Rev AI Python SDK"]},"."]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The following example demonstrates how to submit a remote audio file for transcription."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["To use this example, replace the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["<URL>"]}," placeholder with the public URL to the file you wish to transcribe and the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["<REVAI_ACCESS_TOKEN>"]}," placeholder with your Rev AI account's access token."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"python","header":{"controls":{"copy":{}}},"source":"from rev_ai import apiclient\n\ntoken = \"<REVAI_ACCESS_TOKEN>\"\nsource_url = \"<URL>\"\n\n# create your client\nclient = apiclient.RevAiAPIClient(token)\n\n# submit a job with a link to the source file\njob = client.submit_job_url(source_config=CustomerUrlData(url=source_url))\n\n# check job status\njob_details = client.get_job_details(job.id)\n\n# retrieve transcript as text\ntranscript_text = client.get_transcript_text(job.id)\n\n# retrieve transcript as json\ntranscript_json = client.get_transcript_json(job.id)\n\n# retrieve transcript as a python object\ntranscript_object = client.get_transcript_object(job.id)\n","lang":"python"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"stream-and-transcribe-an-audio-file","__idx":3},"children":["Stream and transcribe an audio file"]},{"$$mdtype":"Tag","name":"Admonition","attributes":{"type":"info"},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["This example uses the ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"/sdk/python"},"children":["Rev AI Python SDK"]},"."]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The following example can be used to configure your streaming client, send audio as a stream from your microphone input, and obtain the transcript as it is processed."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["To use this example, replace the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["<REVAI_ACCESS_TOKEN>"]}," placeholder with your Rev AI access token."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"python","header":{"controls":{"copy":{}}},"source":"import pyaudio\nfrom rev_ai.models import MediaConfig\nfrom rev_ai.streamingclient import RevAiStreamingClient\nfrom six.moves import queue\n\n\"\"\"\nInsert your access token here\n\"\"\"\naccess_token = \"<REVAI_ACCESS_TOKEN>\"\n\nclass MicrophoneStream(object):\n    \"\"\"\n    Opens a recording stream as a generator yielding the audio chunks.\n    \"\"\"\n    def __init__(self, rate, chunk):\n        self._rate = rate\n        self._chunk = chunk\n        \"\"\"\n        Create a thread-safe buffer of audio data\n        \"\"\"\n        self._buff = queue.Queue()\n        self.closed = True\n\n    def __enter__(self):\n        self._audio_interface = pyaudio.PyAudio()\n        self._audio_stream = self._audio_interface.open(\n            format=pyaudio.paInt16,\n            \"\"\"\n            The API currently only supports 1-channel (mono) audio\n            \"\"\"\n            channels=1, rate=self._rate,\n            input=True, frames_per_buffer=self._chunk,\n            \"\"\"\n            Run the audio stream asynchronously to fill the buffer object.\n            This is necessary so that the input device's buffer doesn't\n            overflow while the calling thread makes network requests, etc.\n            \"\"\"\n            stream_callback=self._fill_buffer,\n        )\n\n        self.closed = False\n\n        return self\n\n    def __exit__(self, type, value, traceback):\n        self._audio_stream.stop_stream()\n        self._audio_stream.close()\n        self.closed = True\n        \"\"\"\n        Signal the generator to terminate so that the client's\n        streaming_recognize method will not block the process termination.\n        \"\"\"\n        self._buff.put(None)\n        self._audio_interface.terminate()\n\n    def _fill_buffer(self, in_data, frame_count, time_info, status_flags):\n        \"\"\"\n        Continuously collect data from the audio stream, into the buffer.\n        \"\"\"\n        self._buff.put(in_data)\n        return None, pyaudio.paContinue\n\n    def generator(self):\n        while not self.closed:\n            \"\"\"\n            Use a blocking get() to ensure there's at least one chunk of\n            data, and stop iteration if the chunk is None, indicating the\n            end of the audio stream.\n            \"\"\"\n            chunk = self._buff.get()\n            if chunk is None:\n                return\n            data = [chunk]\n\n            \"\"\"\n            Now consume whatever other data's still buffered.\n            \"\"\"\n            while True:\n                try:\n                    chunk = self._buff.get(block=False)\n                    if chunk is None:\n                        return\n                    data.append(chunk)\n                except queue.Empty:\n                    break\n\n            yield b''.join(data)\n\n\n\"\"\"\nSampling rate of your microphone and desired chunk size\n\"\"\"\nrate = 44100\nchunk = int(rate/10)\n\n\"\"\"\nCreates a media config with the settings set for a raw microphone input\n\"\"\"\nexample_mc = MediaConfig('audio/x-raw', 'interleaved', 44100, 'S16LE', 1)\n\nstreamclient = RevAiStreamingClient(access_token, example_mc)\n\n\"\"\"\nOpens microphone input. The input will stop after a keyboard interrupt.\n\"\"\"\nwith MicrophoneStream(rate, chunk) as stream:\n    \"\"\"\n    Uses try method to enable users to manually close the stream\n    \"\"\"\n    try:\n        \"\"\"\n        Starts the server connection and thread sending microphone audio\n        \"\"\"\n        response_gen = streamclient.start(stream.generator())\n\n        \"\"\"\n        Iterates through responses and prints them\n        \"\"\"\n        for response in response_gen:\n            print(response)\n\n    except KeyboardInterrupt:\n        \"\"\"\n        Ends the WebSocket connection.\n        \"\"\"\n        streamclient.end()\n        pass\n","lang":"python"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"stream-and-transcribe-microphone-audio","__idx":4},"children":["Stream and transcribe microphone audio"]},{"$$mdtype":"Tag","name":"Admonition","attributes":{"type":"info"},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["This example uses the ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"/sdk/python"},"children":["Rev AI Python SDK"]},"."]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The following example can be used to configure your streaming client, send a stream of audio as a generator, and obtain the transcript as the audio is processed."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["To use this example, replace the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["<FILEPATH>"]}," placeholder with the path to the file you wish to transcribe and replace the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["<REVAI_ACCESS_TOKEN>"]}," placeholder with your Rev AI access token."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"python","header":{"controls":{"copy":{}}},"source":"from rev_ai.models import MediaConfig\nfrom rev_ai.streamingclient import RevAiStreamingClient\nimport io\n\n\n\"\"\"\nName of file to be transcribed\n\"\"\"\nfilename = \"<FILEPATH>\"\n\n\"\"\"\nString of your access token\n\"\"\"\naccess_token = \"<REVAI_ACCESS_TOKEN>\"\n\n\"\"\"\nMedia configuration of audio file.\nThis includes the content type, layout, rate, format, and # of channels\n\"\"\"\nconfig = MediaConfig(\"audio/x-raw\", \"interleaved\", 16000, \"S16LE\", 1)\n\n\"\"\"\nCreate client with your access token and media configuration\n\"\"\"\nstreamclient = RevAiStreamingClient(access_token, config)\n\n\"\"\"\nOpen file and read data into array.\nPractically, stream data would be divided into chunks\n\"\"\"\nwith io.open(filename, 'rb') as stream:\n    MEDIA_GENERATOR = [stream.read()]\n\n\"\"\"\nStarts the streaming connection and creates a thread to send bytes from the\nMEDIA_GENERATOR. response_generator is a generator yielding responses from\nthe server\n\"\"\"\nresponse_generator = streamclient.start(MEDIA_GENERATOR)\n\n\"\"\"\nIterates through the responses from the server when obtained\n\"\"\"\nfor response in response_generator:\n    print(response)\n\n\"\"\"\nEnds the connection early. Not needed as the server will close the connection\nupon receiving an \"EOS\" message.\n\"\"\"\nstreamclient.end()\n","lang":"python"},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"create-and-use-a-custom-vocabulary","__idx":5},"children":["Create and use a custom vocabulary"]},{"$$mdtype":"Tag","name":"Admonition","attributes":{"type":"info"},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["This example uses the ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"/sdk/python"},"children":["Rev AI Python SDK"]},"."]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["The following example can be used to create and submit custom vocabularies independently and directly to the custom vocabularies API, as well as check on their progress."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["To use this example, replace the ",{"$$mdtype":"Tag","name":"code","attributes":{},"children":["<REVAI_ACCESS_TOKEN>"]}," placeholder with your Rev AI access token."]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"data-language":"python","header":{"controls":{"copy":{}}},"source":"from rev_ai import custom_vocabularies_client\nfrom rev_ai.models import CustomVocabulary\n\ntoken = \"<REVAI_ACCESS_TOKEN>\"\n\n# create a client\nclient = custom_vocabularies_client.RevAiCustomVocabulariesClient(token)\n\n# construct a CustomVocabulary object using your desired phrases\ncustom_vocabulary = CustomVocabulary([\"Patrick Henry Winston\", \"Robert C Berwick\", \"Noam Chomsky\"])\n\n# submit the CustomVocabulary\ncustom_vocabularies_job = client.submit_custom_vocabularies([custom_vocabulary])\n\n# view the job's progress\njob_state = client.get_custom_vocabularies_information(custom_vocabularies_job['id'])\n\n# get list of previously submitted custom vocabularies\ncustom_vocabularies_jobs = client.get_list_of_custom_vocabularies()\n\n# delete the CustomVocabulary\nclient.delete_custom_vocabulary(custom_vocabularies_job['id'])\n","lang":"python"},"children":[]},{"$$mdtype":"Tag","name":"Admonition","attributes":{"type":"info"},"children":[{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Find ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://github.com/revdotcom/revai-python-sdk/tree/master/examples"},"children":["more examples of using the Rev AI Python SDK on GitHub"]},"."]}]}]},"headings":[{"value":"Python Code Samples","id":"python-code-samples","depth":1},{"value":"Submit a local file for transcription","id":"submit-a-local-file-for-transcription","depth":2},{"value":"Submit a remote file for transcription","id":"submit-a-remote-file-for-transcription","depth":2},{"value":"Stream and transcribe an audio file","id":"stream-and-transcribe-an-audio-file","depth":2},{"value":"Stream and transcribe microphone audio","id":"stream-and-transcribe-microphone-audio","depth":2},{"value":"Create and use a custom vocabulary","id":"create-and-use-a-custom-vocabulary","depth":2}],"frontmatter":{"title":"Python Code Samples","toc":{"enable":true},"seo":{"title":"Python Code Samples"}},"lastModified":"2026-02-24T14:47:49.000Z","pagePropGetterError":{"message":"","name":""}},"slug":"/resources/code-samples/python","userData":{"isAuthenticated":false,"teams":["anonymous"]},"isPublic":true}