Skip to main content

Python server SDK for streaming data into Streamstraight.

Project description

Streamstraight Python SDK

streamstraight-server mirrors the @streamstraight/server Node SDK for producers who prefer Python. It connects to Streamstraight over Socket.IO, manages chunk acknowledgements, and exposes helpers for minting client JWTs.

Quickstart

uv add streamstraight-server

Usage

Streamstraight needs a unique stream_id for every stream you produce. Once connected, you can either push chunks manually with the writer API or hand us an async iterator/object that yields JSON‑serializable chunks. The writer reuses the most recent stream configuration, so call await server.connect({...}) again if you need to swap streams or override the encoder.

Push chunks with the writer context manager

import asyncio
from collections.abc import AsyncIterator

from streamstraight_server import streamstraight_server


async def generate() -> AsyncIterator[str]:
    yield "first"
    yield "second"


async def main() -> None:
    server = await streamstraight_server(
        {"api_key": "YOUR_STREAMSTRAIGHT_API_KEY"},
        {"stream_id": "your-stream-id"},
    )  # connects and configures the stream

    async with server.stream_writer() as writer:
        async for chunk in generate():
            await writer.send(chunk)  # returns an asyncio.Future you can await later if needed
            # You can optionally mirror the same chunks to your HTTP response here.

asyncio.run(main())

Pipe an async iterator directly

import asyncio
from streamstraight_server import streamstraight_server


async def generate():
    # Replace with your LLM or other async generator
    yield {"content": "first chunk"}
    yield {"content": "second chunk"}


async def main() -> None:
    server = await streamstraight_server(
        {"api_key": "YOUR_STREAMSTRAIGHT_API_KEY"},
        {"stream_id": "your-stream-id"},
    )

    await server.stream(generate())

asyncio.run(main())

Mint a client JWT for your browser client

from streamstraight_server import fetch_client_token

token = await fetch_client_token({"api_key": "YOUR_STREAMSTRAIGHT_API_KEY"})

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamstraight_server-0.2.1.tar.gz (14.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streamstraight_server-0.2.1-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file streamstraight_server-0.2.1.tar.gz.

File metadata

File hashes

Hashes for streamstraight_server-0.2.1.tar.gz
Algorithm Hash digest
SHA256 b1a50a42be79a82a62e230828b0f7f710748b3cdadbded2f6754ba6ca61d05d0
MD5 dcb6811d2086ff2dc221a88a6d075e39
BLAKE2b-256 4a785a1683daa5b79c100ce533e21e45c8ffc4e4e435aee7d9423d20d2afec25

See more details on using hashes here.

File details

Details for the file streamstraight_server-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for streamstraight_server-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 502e9ffca3795436e1d98fc3727dc9f414492000022718446352ae1f62419a8b
MD5 8f8bd6ae3f5caaaee3f9c017366f05e6
BLAKE2b-256 66e7d5f3a82304bcd862bf9882d587e2eccb2bdc576ac85c904a6ab348ce44ae

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page