-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch-processing.py
More file actions
60 lines (46 loc) · 1.89 KB
/
batch-processing.py
File metadata and controls
60 lines (46 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
Batch processing pattern — process a list of items through Strale with error handling.
Shows how to loop through items, handle per-item errors, collect results, and
respect rate limits in a real agent workflow.
Use case: Bulk validation, batch enrichment, list processing agents
Requires: STRALE_API_KEY environment variable
Install: pip install straleio
Run: STRALE_API_KEY=sk_live_... python agent-patterns/batch-processing.py
"""
import os
import time
from straleio import Strale
strale = Strale(api_key=os.environ["STRALE_API_KEY"])
EMAILS = [
"alice@stripe.com",
"bob@example.com",
"invalid-email",
"carol@shopify.com",
"dave@disposablemail.com",
]
DELAY_BETWEEN_CALLS = 0.2 # seconds — adjust based on your plan's rate limit
def process_batch(items: list[str]) -> list[dict]:
results = []
for i, email in enumerate(items):
try:
result = strale.do("email-validate", inputs={"email": email}, max_price_cents=5)
results.append({
"input": email,
"valid": result.output.get("valid"),
"sqs": result.sqs.score,
"transaction_id": result.transaction_id,
"error": None,
})
print(f"[{i+1}/{len(items)}] {email} — valid={result.output.get('valid')} SQS={result.sqs.score}")
except Exception as e:
results.append({"input": email, "valid": None, "sqs": None, "error": str(e)})
print(f"[{i+1}/{len(items)}] {email} — error: {e}")
if i < len(items) - 1:
time.sleep(DELAY_BETWEEN_CALLS)
return results
if __name__ == "__main__":
print(f"Processing {len(EMAILS)} emails...\n")
results = process_batch(EMAILS)
valid = sum(1 for r in results if r["valid"] is True)
errors = sum(1 for r in results if r["error"])
print(f"\nDone. {valid}/{len(EMAILS)} valid, {errors} errors")