Metadata-Version: 2.4
Name: patchwell
Version: 0.2.0
Summary: Transcription, data cleaning and cited research for scripts, pipelines and AI agents. Pay per result.
License: MIT
Project-URL: Homepage, https://patchwell.io
Project-URL: Documentation, https://patchwell.io/quickstart
Keywords: transcription,data cleaning,deduplication,research,api,speech-to-text,csv
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

# patchwell

Transcription, data cleaning and cited web research from Python. Pay per result: a failed or refused call costs
nothing. Standard library only, so it runs in Lambda, Airflow, cron jobs and notebooks.

```bash
pip install patchwell
export PATCHWELL_KEY=pw_live_...   # a free key with $1 of credit: https://patchwell.io/#try
```

```python
from patchwell import Patchwell

pw = Patchwell()

# Clean a file, a list of dicts, or several sources merged into one table
clean = pw.clean_data(url="https://example.com/leads.csv")
clean = pw.clean_data(rows=[{"Email": "ANN@X.COM", "Name": "ann lee"}])
clean = pw.clean_data(inputs=[{"url": "https://example.com/crm.csv"}, {"rows": scraped, "name": "web"}])

# Only the rows you haven't sent before, for a daily job
new = pw.clean_data(url=export_url, only_new={"state": "daily-leads", "key": ["Email"]})

# Check rows against your rules and map them onto your import template
ready = pw.clean_data(rows=rows, rules=[{"column": "Age", "range": [0, 120]}],
                      target={"columns": [{"name": "email", "required": True}, {"name": "name"}]})

# Transcribe: waits for long recordings (polls with the server's Retry-After)
t = pw.transcribe("https://example.com/interview.mp3")
print(t["text"])
open("interview.srt", "w").write(t["srt"])

# Research with a cited source for every claim
# ($0.02 per answered question; declined and unanswered questions are free)
a = pw.research("Which EU countries require B2B e-invoicing in 2026?")
print(a["answer"], a["gaps"])

# Hard multi-step questions: depth="deep" searches about twice as wide ($0.06 per answered question).
# private=True keeps the question inside Modal, the cloud Patchwell runs on: same price.
d = pw.research("How old was the author of Dracula when it was published?", depth="deep")
# A worked-out answer's claim has "derived": True, "derived_from" (the facts it rests on) and "calc"

# Results as files
open("clean.xlsx", "wb").write(pw.export(clean, "xlsx"))

# Any tool by name, including tools added after this version: the input is the tool's own JSON body
for name, tool in pw.tools().items():          # every tool, read live from the API
    print(name, tool["price"])
print(pw.describe("research")["input_schema"])   # what a tool takes
r = pw.run("research", ask="Which EU countries require e-invoicing in 2026?", depth="deep")
```

## Retries never charge twice

Every POST carries an `Idempotency-Key`. On a retryable error (`rate_limited`, `unavailable`, a request still running)
the SDK waits for `Retry-After` and sends the same key again, so you get the first response and pay once.
Pass your own `idempotency_key=` (an order or row id) and a later rerun of the same item, by cron or by hand,
returns the first result instead of paying again.

## Errors

```python
from patchwell import PatchwellError

try:
    pw.clean_data(url=url)
except PatchwellError as e:
    if e.code == "insufficient_credit":
        ...                       # top up at https://patchwell.io/#buy
    print(e.code, e.retryable, e.request_id)
```

Codes:
<!-- facts:error-codes-list -->
`invalid_input`, `unauthorized`, `insufficient_credit`, `forbidden`, `not_found`, `method_not_allowed`, `too_large`, `idempotency_in_progress`, `idempotency_mismatch`, `rate_limited`, `source_unavailable`, `unavailable`, `tool_paused`, `internal`, `cancelled`, `expired`, `network`, `timeout`, `job_failed`.
<!-- /facts:error-codes-list -->
`source_unavailable`: the site hosting your file failed for a moment. A job that fails while you wait
raises with the server's own `code` and `retryable`.

## Long jobs without waiting

```python
job = pw.transcribe(url, wait=False)          # {"status": "running", "job_id": "fc-..."}
result = pw.wait(job["job_id"], timeout=3600)
```

Or send `callback_url=` and verify the webhook:

```python
from patchwell import verify_webhook

ok = verify_webhook(secret, request.body, request.headers["Patchwell-Signature"])
```

The full API reference: https://patchwell.io/quickstart
