Skip to main content

slot-flight Python

Python SDK for slot-wise LLM value streaming with server-owned JSON assembly.

This package implements the same slot frame protocol used by the TypeScript SDK. The core engine is provider-independent, and the public object API is Pydantic first. Provider/framework adapters are available for the OpenAI SDK, OpenAI-compatible HTTP endpoints, and LangChain. Runnable examples live in examples/.

Full Python SDK notes live in the Python SDK guide.

import os

from openai import AsyncOpenAI
from pydantic import BaseModel, Field
from slot_flight import slot_object
from slot_flight.adapters.openai import stream_slot_object


class Triage(BaseModel):
    summary: str = Field(description="Write one concise operational summary.")
    tags: list[str] = Field(description="Write exactly 3 tags, one per frame.")


openai = AsyncOpenAI(
    api_key=os.getenv("API_KEY"),
    base_url=os.getenv("API_BASE_URL"),
)

stream = stream_slot_object(
    client=openai,
    model=os.getenv("MODEL", "openai/gpt-oss-20b"),
    messages=[{"role": "user", "content": "Classify this feedback."}],
    output=slot_object(Triage),
)

async for slot in stream.completed_slot_stream():
    print(slot.slot, slot.value)

result = await stream.final_object()

Slot Object API

slot_object() accepts a Pydantic v2 model. Every generated leaf field must use Field(description=...); that description becomes the model-facing instruction for the slot.

class Triage(BaseModel):
    summary: str = Field(description="Write one concise operational summary.")
    tags: list[str] = Field(description="Write exactly 3 tags, one per frame.")

The model always emits raw slot values, not JSON objects or arrays. Pydantic objects and arrays are expanded into structural slots such as metadata.audience, tags[], and sections[].heading; repeat values use indexed frames such as <2:0>...</2:0>, and the engine maps those indexes to JSON paths during assembly.

Lists of models stream field-by-field, so partial updates preserve object structure while each leaf value is still validated independently. Dynamic mapping fields are not inferred because their keys are not known ahead of streaming; model those outputs as explicit fields or handle them outside slot_object().

Failed slot validation retries only the failed slots up to max_retries:

output = slot_object(Triage, max_retries=1)

The object stream exposes the same views as the TypeScript SDK, using Python method names:

async for event in stream.slot_event_stream():
    ...

async for partial in stream.partial_object_stream():
    ...

async for chunk in stream.to_sse(source="completed"):
    ...

async for line in stream.to_ndjson(source="events"):
    ...

final_object(), completed_slot_stream(), partial_object_stream(), and slot_event_stream() consume one underlying model run. Choose one live stream view per run. After that view finishes, final_object() can still return the final state or rethrow the terminal stream error.

If you already have a slot event stream, wrap it with create_slot_object_event_stream() to reuse the same object-stream views:

from slot_flight import create_slot_object_event_stream

stream = create_slot_object_event_stream(existing_events)

HTTP Streaming

to_sse() and to_ndjson() are framework-neutral async iterators. In FastAPI or Starlette, pass them to StreamingResponse:

from starlette.responses import StreamingResponse


async def route():
    stream = stream_slot_object(...)
    return StreamingResponse(
        stream.to_sse(source="completed"),
        media_type="text/event-stream",
    )

source="completed" emits completed slot updates plus retry/error/done events. source="partial" emits draft object snapshots. source="events" emits the low-level slot lifecycle events.

Examples

uv run --extra openai examples/openai_compatible.py
uv run --extra openai-compatible examples/openai_compatible_httpx.py
uv run --extra langchain examples/langchain_runnable.py

The OpenAI SDK example and raw OpenAI-compatible HTTP example both work with endpoints such as NVIDIA NIM: set API_KEY, API_BASE_URL, and MODEL as shown in the root .env.example. The raw HTTP adapter uses a bounded HTTPX timeout by default; pass timeout=None to disable it, or pass a custom httpx.AsyncClient as client when you want to own HTTP settings directly.

Development

uv sync --all-extras --dev
uv run ruff check .
uv run ty check
uv run pytest
uv build

Metadata

Release files for slot-flight 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for slot-flight 0.2.1
File Size Uploaded
slot_flight-0.2.1.tar.gz 23.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for slot-flight 0.2.1
File Interpreter ABI Platform
slot_flight-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 52.0 kB

Release files / slot_flight-0.2.1.tar.gz

Download URL slot_flight-0.2.1.tar.gz
Size 23.1 kB
Tags Source
SHA-256 checksum
How to use checksums
fb19caa4621e154b3a2ddb92cfc261d0b858eea73efbd0537ce2ac6463262db7
BLAKE2b-256 checksum
How to use checksums
54c3a7f712dc6e2db70e2906ed2c9d2d3e2a024874b73d8105197978fc7cc652
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 7, 2026.

Transparency log

Release files / slot_flight-0.2.1-py3-none-any.whl

Download URL slot_flight-0.2.1-py3-none-any.whl
Size 28.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2e6360894ff0aaf325423f618adde1dcccd921fa579cf64aac47501159e390bc
BLAKE2b-256 checksum
How to use checksums
03cebdba24cc2d21389e8fb1c5272f8ecbec82d062db318ac3b31d8c0b25f659
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 7, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page