Python SDK (polylingo)

Officiële Python-client voor de PolyLingo REST API. Het gebruikt httpx en biedt zowel sync als async clients met dezelfde methodenamen.

Voor ruwe HTTP-details, zie API-referentie.


Installatie

pip install polylingo

Python: >= 3.9


Sync client

import os
import polylingo

client = polylingo.PolyLingo(
    api_key=os.environ["POLYLINGO_API_KEY"],
    base_url="https://api.usepolylingo.com/v1",  # optioneel; standaard weergegeven
    timeout=120.0,  # optioneel; seconden per verzoek (standaard 120)
)

result = client.translate(content="# Hello", targets=["es", "fr"], format="markdown")
print(result["translations"]["es"])

client.close()

Contextmanager:

with polylingo.PolyLingo(api_key="...") as client:
    print(client.languages())
ArgumentVerplichtBeschrijving
api_keyJaAPI-sleutel (Authorization: Bearer …).
base_urlNeeAPI-prefix inclusief /v1. Standaard https://api.usepolylingo.com/v1.
timeoutNeehttpx-timeout in seconden. Standaard 120.0.

Async client

import polylingo

async with polylingo.AsyncPolyLingo(api_key="...") as client:
    r = await client.translate(content="Hi", targets=["de"])

Gebruik await client.aclose() als je geen async with gebruikt.

Methodenamen komen overeen met de sync client; alle netwerkmethoden zijn async def.


Methoden (sync en async)

health() / await health()

GET /health

h = client.health()
# async: h = await client.health()

languages() / await languages()

GET /languages

data = client.languages()
langs = data["languages"]

translate(...)

POST /translate

r = client.translate(
    content="# Hello",
    targets=["es", "fr"],
    format="markdown",  # optioneel
    source="en",        # optioneel
    model="standard",   # optioneel: "standard" | "advanced"
)
r["translations"]["es"]
r["usage"]["total_tokens"]

batch(...)

POST /translate/batch

b = client.batch(
    items=[
        {"id": "a", "content": "Hello"},
        {"id": "b", "content": "## Title", "format": "markdown"},
    ],
    targets=["de"],
)
b["results"][0]["translations"]["de"]

usage() / await usage()

GET /usage

u = client.usage()

Jobs — client.jobs

create / await create

POST /jobs — retourneert de 202 body (job_id, status, …).

job = client.jobs.create(content=long_md, targets=["de", "fr"], format="markdown")
# kwargs ook geaccepteerd: client.jobs.create(**{"content": ..., "targets": [...]})

get(job_id) / await get(job_id)

GET /jobs/:id. Wanneer status == "completed", bevatten de responses translations en usage op het hoogste niveau.

translate(...) — gemaksmethode

Pollt totdat completed of failed, of totdat de tijd om is.

done = client.jobs.translate(
    content=long_md,
    targets=["de", "fr", "es"],
    format="markdown",
    poll_interval=10.0,   # seconden tussen polls; standaard 5.0
    timeout=600.0,        # **totaal** seconden budget; standaard 1200 (20 min)
    on_progress=lambda pos: print(f"Queue: {pos}"),
)
done["translations"]["de"]

Async:

done = await client.jobs.translate(
    content=long_md,
    targets=["de"],
    poll_interval=2.0,
    timeout=300.0,
)

API-statussen: pending, processing, completed, failed.


Excepties

KlasseWanneer
polylingo.PolyLingoErrorBasis — status, error, args[0] bericht.
polylingo.AuthErrorHTTP 401.
polylingo.RateLimitErrorHTTP 429retry_after kan ingesteld zijn (seconden).
polylingo.JobFailedErrorMislukte job, slechte voltooide payload, of polling timeout — job_id.
import polylingo

try:
    client.translate(content="x", targets=["es"])
except polylingo.AuthError as e:
    print(e.status, e.error)
except polylingo.RateLimitError as e:
    print(e.retry_after)
except polylingo.JobFailedError as e:
    print(e.job_id)
except polylingo.PolyLingoError as e:
    print(e.status, e.error)

Async jobs patroon (samenvatting)

  1. Handmatig: jobs.create → loop jobs.get tot terminale status.
  2. Helper: jobs.translate met poll_interval, timeout, en optioneel on_progress.

Geef de voorkeur aan jobs voor zeer grote inhoud waarbij synchrone translate mogelijk client- of server-timeouts veroorzaakt.


Types

Het pakket bevat py.typed. Response-objecten zijn gewone dict-waarden die overeenkomen met de API; gebruik TypedDict-stijl annotaties in je code indien gewenst.


Changelog

0.1.0

  • Eerste release: sync PolyLingo, async AsyncPolyLingo, volledige endpoint-dekking inclusief jobs.translate polling helper.
Python SDK | PolyLingo docs | PolyLingo