Skip to main content

Python client for the LatentKit gateway (OpenAI-compatible /v1 HTTP API).

Project description

LatentKit Python SDK

Official Python client for the canonical LatentKit /v1 API.

Install

pip install latentkit

Requires Python 3.10+.

Quickstart

from latentkit import LatentKit, LatentKitAPIError

client = LatentKit(api_key="YOUR_RAW_KEY")
try:
    response = client.chat.create(
        messages=[{"role": "user", "content": "Say hello from LatentKit."}],
        max_tokens=100,
        response_profile="balanced",
    )
    print(response["content"])
except LatentKitAPIError as exc:
    print(exc.status_code, exc.body)
client.close()

Async

import asyncio

from latentkit import AsyncLatentKit


async def main() -> None:
    async with AsyncLatentKit(api_key="YOUR_RAW_KEY") as client:
        response = await client.completions.create(
            prompt="Write a short product description for LatentKit.",
            system="Respond in one sentence.",
        )
        print(response["content"])


asyncio.run(main())

Timeouts and custom clients

LatentKit(...) and AsyncLatentKit(...) create httpx clients with a default timeout of 120s.

If you inject your own http_client, configure timeouts on that client yourself:

import httpx
from latentkit import LatentKit

http_client = httpx.Client(timeout=30.0)
client = LatentKit(api_key="YOUR_RAW_KEY", http_client=http_client)

Streaming

from latentkit import LatentKit

with LatentKit(api_key="YOUR_RAW_KEY") as client:
    for event in client.chat.stream(
        messages=[{"role": "user", "content": "Count from one to five."}],
    ):
        if event.event == "error":
            raise RuntimeError(event.data)
        if event.is_done:
            break
        print(event.data["delta"], end="")

Response profiles

Pass response_profile to ask the assigned policy for a speed/depth tradeoff:

response = client.chat.create(
    messages=[{"role": "user", "content": "Give me the short version."}],
    response_profile="fast",
)

Allowed values are fast, balanced, and deep. The assigned policy controls whether request overrides are allowed and which routes are eligible for each profile.

Agent sessions

from latentkit import LatentKit

with LatentKit(api_key="YOUR_RAW_KEY") as client:
    session = client.agents.sessions.create(
        task="Inspect the repo and explain the auth flow",
        workspace_root="/workspace",
        permission_mode="workspace-write",
    )
    queued = client.agents.sessions.run(session["id"])
    print(queued)

See docs/latentkit-coder-api.md for the full agent session request/response model.

Modalities

with LatentKit(api_key="YOUR_RAW_KEY") as client:
    client.embeddings.create(input=["hello world"], dimensions=256)
    client.image.generate(prompt="A clean product icon", size="1024x1024")
    client.speech.create(input="Hello from LatentKit.", voice="alloy")
    client.transcription.create(audio={"base64": "..."}, language="en")
    client.translation.create(audio={"base64": "..."}, target_language="en")
    client.video.generate(prompt="A short product scene", duration_seconds=4)

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

latentkit-0.1.0.tar.gz (5.9 MB view details)

Uploaded Source

File details

Details for the file latentkit-0.1.0.tar.gz.

File metadata

  • Download URL: latentkit-0.1.0.tar.gz
  • Upload date:
  • Size: 5.9 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for latentkit-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7f1092b42ba4fa75452c7cca50a96e77147613f8021cdb55c7351c1095009192
MD5 acab0f83ce5ebd7efa5d8922a51a64e9
BLAKE2b-256 920e5b1a9bef5b9c6da6fb54f2d4ea73ff68287482553d0b6ad75f147834b3a8

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