Skip to main content

Slot-wise LLM generation and server-owned JSON assembly for Python.

Project description

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 a JSON array of exactly 3 tags.")


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 a JSON array of exactly 3 tags.")

String fields, string enums, and string literals use text slots. Other described fields use JSON slots and are parsed before Pydantic validates the value. A nested model without its own field description is expanded into nested slots; a nested model with a description becomes one JSON slot.

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. After the run finishes, later consumers replay cached events. A second live consumer is rejected while a run is still active.

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

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

slot_flight-0.1.1.tar.gz (19.9 kB view details)

Uploaded Source

Built Distribution

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

slot_flight-0.1.1-py3-none-any.whl (25.4 kB view details)

Uploaded Python 3

File details

Details for the file slot_flight-0.1.1.tar.gz.

File metadata

  • Download URL: slot_flight-0.1.1.tar.gz
  • Upload date:
  • Size: 19.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for slot_flight-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1c1f5923c9b5d3457a76c6eafcbafed49be1f39c174ce07ade23e1f4ca5afc8f
MD5 d772b2f11483c608b3c2d36366720c07
BLAKE2b-256 368e675a56f8e24603aa1ae2090e10c5b0a15589b12df44d256dbabd2e5c8481

See more details on using hashes here.

Provenance

The following attestation bundles were made for slot_flight-0.1.1.tar.gz:

Publisher: python-release.yml on miinhho/slot-flight

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file slot_flight-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: slot_flight-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 25.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for slot_flight-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e271260277eb0002715b28f8d67824e1c229d61a7ae9c021c919485206e8f7c7
MD5 fa9343e7950b0ee49da34e50ac0b66c7
BLAKE2b-256 95181c1308cb5d52bbc333a5da85622fff9028409e8c85f2b979e26b0da19d83

See more details on using hashes here.

Provenance

The following attestation bundles were made for slot_flight-0.1.1-py3-none-any.whl:

Publisher: python-release.yml on miinhho/slot-flight

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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