batch and abatch
batch() and abatch() apply one Runnable to several independent inputs. The base batch() uses a thread pool; the base abatch() uses asyncio.gather. An integration may override them with a provider-native batch operation.
Tiny example
Section titled “Tiny example”tickets = ["INC-101", "INC-102", "INC-103"]
results = await model.abatch( [f"Classify {ticket}" for ticket in tickets], config={"max_concurrency": 3}, return_exceptions=True,)
for ticket, result in zip(tickets, results): print(ticket, result)Remember the flow
Section titled “Remember the flow”- Use independent inputs.
- Set
max_concurrencyto protect the provider and your own service. - Match outputs to inputs by position, or use an “as completed” method and its returned index.
- Decide whether one error stops the batch or is returned beside other results.
Failure note
Section titled “Failure note”Batching is not automatically a provider’s discounted offline batch API. It can simply make concurrent client calls. Unbounded concurrency can trigger 429 responses, exhaust sockets, and amplify retries.