Skip to main content

Reflow (Python)

Ported line-for-line from the TypeScript implementation. See the root README for the pitch, the prior-art table, and the scope decisions — this file only covers what's specific to running the Python code.

Install

pip install -e ".[dev]"

Quickstart

PYTHONPATH=src python3 examples/quickstart.py
from reflow import Reflow

async def on_prompt_too_long(messages):
    return (await context_clip.recover(messages)).messages

reflow = Reflow(
    on_prompt_too_long=on_prompt_too_long,
    on_media_too_large=lambda messages: strip_large_images(messages),
    on_truncated=lambda messages, attempt: [*messages, continue_message],
    max_truncation_retries=2,
)

result = await reflow.call(
    lambda messages: anthropic.messages.create(model=model, messages=messages, max_tokens=4096),
    initial_messages,
)
# result.value        — the response
# result.recoveries   — ordered log of what happened, e.g. ["prompt_too_long"]
# result.truncated    — True if still truncated after exhausting retries

Test

pytest

Status

Prompt-too-long recovery, media-too-large recovery, and bounded truncation recovery are real and tested, with default classifiers for common Anthropic/OpenAI-shaped errors. Not yet published to PyPI — the bare reflow name is taken by an unrelated abandoned package, so this publishes as reflowkit instead, same as the TypeScript package.

Release files for reflowkit 0.0.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 reflowkit 0.0.1
File Size Uploaded
reflowkit-0.0.1.tar.gz 6.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for reflowkit 0.0.1
File Interpreter ABI Platform
reflowkit-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 11.7 kB

Release files / reflowkit-0.0.1.tar.gz

Download URL reflowkit-0.0.1.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
8dfceff1a2cb25d80b92a55b7c892385bda075defa5de441733eaf1569f1dbc8
BLAKE2b-256 checksum
How to use checksums
68a0bd6342c8d9c77d1e51f7d7eb375b4d796134eb2b2c61f890474ac24c3192
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.8

Release files / reflowkit-0.0.1-py3-none-any.whl

Download URL reflowkit-0.0.1-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
554884c4579864e6df78643ebad9cc006270a70427fc771d21a3d54b3fc48161
BLAKE2b-256 checksum
How to use checksums
35aa618128a307b6cf514fd723d37c34a3c1b67da21bcd8a61598f8365fced8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.8

Release history Release notifications | RSS feed

0.0.3

2 release files

0.0.2

2 release files

This release

0.0.1 This release

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