HTTP clients¶
Client and AsyncClient share an identical method surface — anything you
can do with one, you can await with the other.
Construct either directly with base_url, from env vars with
Client.from_env(), or pass a pre-built httpx.Client via http_client=.
The introspection properties (.base_url, .auth_scheme, .default_headers,
.timeout, .retry) read back the resolved config.
Client¶
Client
¶
Client(*, base_url: str | None = None, api_key: str | None = None, auth_scheme: AuthScheme = 'bearer', timeout: float = DEFAULT_TIMEOUT, default_headers: Mapping[str, str] | None = None, retry: RetryPolicy | None = None, transport: BaseTransport | None = None, http_client: Client | None = None, limits: Limits | None = None, on_request: SyncRequestHook | None = None, on_response: SyncResponseHook | None = None)
Bases: _BaseClient
Synchronous HTTP client for the Turbo-OCR server.
Holds a pooled httpx.Client and the auth / retry configuration used
by every request method. Prefer instantiating with with Client(...) as
client: so the underlying connection pool is closed deterministically.
Build a Client and (unless http_client is supplied) its httpx.Client.
| PARAMETER | DESCRIPTION |
|---|---|
base_url
|
Server origin, e.g.
TYPE:
|
api_key
|
Secret used for bearer auth (or
TYPE:
|
auth_scheme
|
TYPE:
|
timeout
|
Per-request timeout in seconds, applied to the
underlying
TYPE:
|
default_headers
|
Extra headers merged into every request (e.g.
tenant tags, trace IDs). Authorization headers derived from
TYPE:
|
retry
|
TYPE:
|
transport
|
Optional
TYPE:
|
http_client
|
Pre-configured
TYPE:
|
limits
|
Connection pool sizing. Defaults to
TYPE:
|
on_request
|
Synchronous hook
TYPE:
|
on_response
|
Synchronous hook
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
InvalidParameter
|
If |
| METHOD | DESCRIPTION |
|---|---|
from_env |
Construct a |
close |
Close the underlying HTTP session. Use the context manager when possible. |
recognize_image |
OCR a single image and return text plus optional layout structure. |
recognize_base64 |
OCR an image whose bytes are already base64-encoded. |
recognize_pixels |
OCR a raw RGB/RGBA pixel buffer, skipping image-decode on both ends. |
recognize_batch |
OCR multiple images in a single round-trip. |
recognize_pdf |
OCR every page of a PDF. |
health |
Probe the server's health endpoints. |
make_searchable_pdf |
Return a PDF with an invisible OCR text layer. |
to_markdown |
One-call OCR + layout + reading-order + blocks → Markdown. |
from_env
classmethod
¶
Construct a Client driven by TURBO_OCR_BASE_URL / TURBO_OCR_API_KEY env vars.
Plain Client() already reads those env vars when the corresponding
kwarg is None. This factory exists for callers who want the
env-aware origin to be explicit in their code. Pass keyword overrides
to take precedence over the env values.
recognize_image
¶
recognize_image(image: ImageInput, *, layout: BoolParam = None, reading_order: BoolParam = None, include_blocks: BoolParam = None, timeout: float | None = None) -> OcrResponse
OCR a single image and return text plus optional layout structure.
Accepts the broadest possible set of image-shaped inputs: file paths
(str / Path), in-memory bytes, file-like objects with .read(),
numpy arrays, and PIL Image instances. The bytes are POSTed as
multipart/form-data; image decoding happens server-side.
| PARAMETER | DESCRIPTION |
|---|---|
image
|
The image to recognize. See
TYPE:
|
layout
|
Pass
TYPE:
|
reading_order
|
Pass
TYPE:
|
include_blocks
|
Pass
TYPE:
|
timeout
|
Per-request timeout override in seconds.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
OcrResponse
|
An |
OcrResponse
|
boxes), plus |
OcrResponse
|
and |
| RAISES | DESCRIPTION |
|---|---|
InvalidParameter
|
Malformed input (e.g. unsupported image format, missing file). |
ImageDecodeError
|
The server could not decode the bytes as an image. |
DimensionsTooLarge
|
Image exceeds the server's pixel budget. |
APIConnectionError
|
Network failure, timeout, or unparseable response after the retry policy is exhausted. |
ServerError
|
5xx response that is not retryable. |
recognize_base64
¶
recognize_base64(b64: str, *, layout: BoolParam = None, reading_order: BoolParam = None, include_blocks: BoolParam = None, timeout: float | None = None) -> OcrResponse
OCR an image whose bytes are already base64-encoded.
Useful when the image arrived over a JSON wire and you'd rather not
round-trip through b64decode just to hand it back to the SDK.
| PARAMETER | DESCRIPTION |
|---|---|
b64
|
Base64-encoded image bytes (the raw payload — no
TYPE:
|
layout
|
Same semantics as
TYPE:
|
reading_order
|
Same semantics as
TYPE:
|
include_blocks
|
Same semantics as
TYPE:
|
timeout
|
Per-request timeout override in seconds.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
OcrResponse
|
An |
OcrResponse
|
what |
OcrResponse
|
returns. |
| RAISES | DESCRIPTION |
|---|---|
InvalidParameter
|
Malformed or non-base64 input. |
ImageDecodeError
|
Decoded bytes are not a valid image. |
APIConnectionError
|
Network failure, timeout, or unparseable response. |
ServerError
|
5xx response. |
recognize_pixels
¶
recognize_pixels(pixels: ImageInput, *, width: int, height: int, channels: int = 3, layout: BoolParam = None, reading_order: BoolParam = None, include_blocks: BoolParam = None, timeout: float | None = None) -> OcrResponse
OCR a raw RGB/RGBA pixel buffer, skipping image-decode on both ends.
The server's JPEG/PNG decoder is bypassed, which removes ~5-15 ms per request for pipelines that already hold uncompressed pixels.
| PARAMETER | DESCRIPTION |
|---|---|
pixels
|
Raw pixel buffer. Pass
TYPE:
|
width
|
Image width in pixels. Must match the buffer's row stride.
TYPE:
|
height
|
Image height in pixels.
TYPE:
|
channels
|
Number of channels per pixel —
TYPE:
|
layout
|
Same semantics as
TYPE:
|
reading_order
|
Same semantics as
TYPE:
|
include_blocks
|
Same semantics as
TYPE:
|
timeout
|
Per-request timeout override in seconds.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
OcrResponse
|
An |
| RAISES | DESCRIPTION |
|---|---|
InvalidParameter
|
Dimensions or channel count are invalid, or
|
DimensionsTooLarge
|
Image exceeds the server's pixel budget. |
APIConnectionError
|
Network failure or timeout. |
ServerError
|
5xx response. |
recognize_batch
¶
recognize_batch(images: Iterable[ImageInput], *, layout: BoolParam = None, reading_order: BoolParam = None, include_blocks: BoolParam = None, timeout: float | None = None) -> BatchResponse
OCR multiple images in a single round-trip.
Per-slot failures are surfaced on
BatchResponse.errors (a parallel
list[str | None]) so one bad input never fails the whole batch.
| PARAMETER | DESCRIPTION |
|---|---|
images
|
Iterable of image inputs (mixed types are fine — paths, bytes, file-like objects, numpy arrays, PIL images may all appear in the same batch).
TYPE:
|
layout
|
Applied uniformly to every image. Same semantics as
TYPE:
|
reading_order
|
Applied uniformly to every image.
TYPE:
|
include_blocks
|
Applied uniformly to every image.
TYPE:
|
timeout
|
Per-request timeout override in seconds.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
BatchResponse
|
A |
BatchResponse
|
|
BatchResponse
|
length as |
BatchResponse
|
|
BatchResponse
|
for tagged |
BatchResponse
|
zipping the two lists by hand. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
Empty input. The server rejects empty batches — feed at least one image, or skip the call entirely. |
APIConnectionError
|
Network failure or timeout on the
round-trip itself (per-slot decode failures land in
|
ServerError
|
5xx response. |
recognize_pdf
¶
recognize_pdf(pdf: ImageInput, *, dpi: int = 150, mode: PdfMode | str | None = None, layout: BoolParam = None, reading_order: BoolParam = None, include_blocks: BoolParam = None, timeout: float | None = None) -> PdfResponse
OCR every page of a PDF.
| PARAMETER | DESCRIPTION |
|---|---|
pdf
|
PDF bytes, path, or file-like object. See
TYPE:
|
dpi
|
Server-side rasterization DPI (default
TYPE:
|
mode
|
PDF reader strategy. One of:
TYPE:
|
layout
|
Per-page layout. Same semantics as
TYPE:
|
reading_order
|
Per-page reading order.
TYPE:
|
include_blocks
|
Per-page block grouping.
TYPE:
|
timeout
|
Per-request timeout override in seconds.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
PdfResponse
|
A |
PdfResponse
|
|
PdfResponse
|
|
| RAISES | DESCRIPTION |
|---|---|
InvalidParameter
|
|
PdfRenderError
|
Server failed to rasterize the PDF (corrupt file, encrypted without a password, etc.). |
APIConnectionError
|
Network failure or timeout. |
ServerError
|
5xx response. |
health
¶
health(*, ready: bool = False, live: bool = False) -> HealthStatus
Probe the server's health endpoints.
| PARAMETER | DESCRIPTION |
|---|---|
ready
|
Hit
TYPE:
|
live
|
Hit
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
HealthStatus
|
A |
HealthStatus
|
status code, raw body, and (when JSON) the parsed body. |
| RAISES | DESCRIPTION |
|---|---|
InvalidParameter
|
Both |
APIConnectionError
|
Network failure or timeout (note: HTTP
errors are reflected as |
make_searchable_pdf
¶
make_searchable_pdf(source: ImageInput, *, dpi: int = 200, mode: PdfMode | str | None = None, font_path: str | None = None) -> bytes
Return a PDF with an invisible OCR text layer.
Accepts a PDF or a single-page image. Tested input formats: PDF,
PNG, JPEG, BMP, TIFF, GIF, WebP. The SDK detects format from the
first bytes and dispatches to
recognize_pdf or
recognize_image accordingly,
wrapping the image into a one-page PDF when needed.
Output is visually identical to the input; the text is selectable, copyable, and full-text-searchable in every viewer. The bundled glyphless font covers every Basic Multilingual Plane codepoint so non-Latin scans (CJK, Arabic, Cyrillic, …) work with no font setup.
| PARAMETER | DESCRIPTION |
|---|---|
source
|
PDF or image bytes / path / file-like object. See
TYPE:
|
dpi
|
Rasterization DPI for PDF inputs and the page dimension
used when wrapping an image input (default
TYPE:
|
mode
|
PDF reader strategy; see
TYPE:
|
font_path
|
Absolute path to a custom
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bytes
|
The output PDF as |
| RAISES | DESCRIPTION |
|---|---|
FontGlyphMissing
|
Only when you pass a |
PdfRenderError
|
Server failed to rasterize the PDF. |
APIConnectionError
|
Network failure or timeout on the underlying recognise call. |
ServerError
|
5xx response. |
to_markdown
¶
to_markdown(image_or_pdf: ImageInput, *, dpi: int = 150, mode: PdfMode | str | None = None, style: MarkdownStyle | None = None, timeout: float | None = None) -> MarkdownDocument
One-call OCR + layout + reading-order + blocks → Markdown.
Detects PDF vs image by the magic bytes b"%PDF-", then calls
recognize_pdf or
recognize_image with
include_blocks=True and pipes the response through
render_to_markdown.
| PARAMETER | DESCRIPTION |
|---|---|
image_or_pdf
|
Image or PDF input. See
TYPE:
|
dpi
|
Rasterization DPI for the PDF branch (default
TYPE:
|
mode
|
PDF reader strategy for the PDF branch; see
TYPE:
|
style
|
TYPE:
|
timeout
|
Per-request timeout override in seconds.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
MarkdownDocument
|
A |
MarkdownDocument
|
rendered |
| RAISES | DESCRIPTION |
|---|---|
ProtocolError
|
Server returned a layout-enabled response with
inconsistent IDs (text item missing |
InvalidParameter
|
Underlying recognise call rejected the input. |
APIConnectionError
|
Network failure or timeout. |
ServerError
|
5xx response. |
AsyncClient¶
AsyncClient
¶
AsyncClient(*, base_url: str | None = None, api_key: str | None = None, auth_scheme: AuthScheme = 'bearer', timeout: float = DEFAULT_TIMEOUT, default_headers: Mapping[str, str] | None = None, retry: RetryPolicy | None = None, transport: AsyncBaseTransport | None = None, http_client: AsyncClient | None = None, limits: Limits | None = None, on_request: AsyncRequestHook | None = None, on_response: AsyncResponseHook | None = None)
Bases: _BaseClient
Asynchronous HTTP client for the Turbo-OCR server.
Mirror of Client over httpx.AsyncClient. Every
request method is the awaitable counterpart of the sync one; see the
Client method for full docs.
| METHOD | DESCRIPTION |
|---|---|
from_env |
Async equivalent of |
aclose |
Close the underlying async HTTP session. Prefer |
recognize_image |
Async equivalent of |
recognize_base64 |
Async equivalent of |
recognize_pixels |
Async equivalent of |
recognize_batch |
Async equivalent of |
recognize_pdf |
Async equivalent of |
health |
Async equivalent of |
make_searchable_pdf |
Async equivalent of |
to_markdown |
Async equivalent of |
from_env
classmethod
¶
Async equivalent of Client.from_env.
aclose
async
¶
Close the underlying async HTTP session. Prefer async with over this.
recognize_image
async
¶
recognize_image(image: ImageInput, *, layout: BoolParam = None, reading_order: BoolParam = None, include_blocks: BoolParam = None, timeout: float | None = None) -> OcrResponse
Async equivalent of Client.recognize_image.
recognize_base64
async
¶
recognize_base64(b64: str, *, layout: BoolParam = None, reading_order: BoolParam = None, include_blocks: BoolParam = None, timeout: float | None = None) -> OcrResponse
Async equivalent of Client.recognize_base64.
recognize_pixels
async
¶
recognize_pixels(pixels: ImageInput, *, width: int, height: int, channels: int = 3, layout: BoolParam = None, reading_order: BoolParam = None, include_blocks: BoolParam = None, timeout: float | None = None) -> OcrResponse
Async equivalent of Client.recognize_pixels.
recognize_batch
async
¶
recognize_batch(images: Iterable[ImageInput], *, layout: BoolParam = None, reading_order: BoolParam = None, include_blocks: BoolParam = None, timeout: float | None = None) -> BatchResponse
Async equivalent of Client.recognize_batch.
recognize_pdf
async
¶
recognize_pdf(pdf: ImageInput, *, dpi: int = 150, mode: PdfMode | str | None = None, layout: BoolParam = None, reading_order: BoolParam = None, include_blocks: BoolParam = None, timeout: float | None = None) -> PdfResponse
Async equivalent of Client.recognize_pdf.
health
async
¶
health(*, ready: bool = False, live: bool = False) -> HealthStatus
Async equivalent of Client.health.
make_searchable_pdf
async
¶
make_searchable_pdf(source: ImageInput, *, dpi: int = 200, mode: PdfMode | str | None = None, font_path: str | None = None) -> bytes
Async equivalent of Client.make_searchable_pdf.
to_markdown
async
¶
to_markdown(image_or_pdf: ImageInput, *, dpi: int = 150, mode: PdfMode | str | None = None, style: MarkdownStyle | None = None, timeout: float | None = None) -> MarkdownDocument
Async equivalent of Client.to_markdown.