Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Groq Groq python BinaryAPIResponse Usage

From Leeroopedia
Knowledge Sources
Domains API_Client, File_Handling
Last Updated 2026-02-15 16:00 GMT

Overview

Concrete response wrapper for handling binary API responses in the Groq Python SDK.

Description

BinaryAPIResponse extends the base APIResponse class to provide binary-specific access methods. It wraps the httpx.Response object and provides .content for direct byte access, .write_to_file() for saving to disk, and .stream_to_file() for streaming large responses.

Used by Speech.create() (TTS audio) and Files.content() (batch result files).

Usage

Returned automatically by API methods that produce binary output. Use .content for in-memory access or .write_to_file() for file persistence.

Code Reference

Source Location

  • Repository: groq-python
  • File: src/groq/_response.py
  • Lines: L1-830 (full response module; BinaryAPIResponse inherits from APIResponse)

Signature

class BinaryAPIResponse:
    # Inherited from APIResponse
    content: bytes               # Raw response bytes
    headers: httpx.Headers       # Response headers
    status_code: int             # HTTP status code

    def write_to_file(self, path: str | Path) -> None: ...
    def stream_to_file(self, path: str | Path) -> None: ...

Import

from groq._response import BinaryAPIResponse

I/O Contract

Inputs

Name Type Required Description
(object) BinaryAPIResponse Yes Returned from Speech.create() or Files.content()

Outputs

Name Type Description
.content bytes Raw binary content
.write_to_file(path) None Saves content to file at given path
.stream_to_file(path) None Streams content to file (memory-efficient for large files)
.headers httpx.Headers HTTP response headers

Usage Examples

Save TTS Audio

response = client.audio.speech.create(
    input="Hello world",
    model="playai-tts",
    voice="Arista-PlayAI",
)

# Method 1: Direct bytes
audio_bytes = response.content
with open("output.wav", "wb") as f:
    f.write(audio_bytes)

# Method 2: Convenience method
response.write_to_file("output.wav")

Download Batch Results

result = client.files.content(output_file_id)
content = result.content.decode("utf-8")
for line in content.strip().split("\n"):
    import json
    result_obj = json.loads(line)
    print(result_obj)

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment