@@ -85,11 +85,28 @@ class OpenRouterTransport:
8585 """A BatchTransport backed by the OpenRouter Python SDK."""
8686
8787 def __init__ (self , client : OpenRouter , model : str ) -> None :
88+ """Initialize the transport with an OpenRouter client and model.
89+
90+ Args:
91+ client (OpenRouter): The OpenRouter client to use.
92+ model (str): The model identifier.
93+ """
8894 _ensure_batch_body_field_order ()
8995 self ._client = client
9096 self ._model = model
9197
9298 def create (self , requests : Sequence [BatchRequest ]) -> BatchSubmission :
99+ """Submit chat completion requests as an OpenRouter batch.
100+
101+ Args:
102+ requests (Sequence[BatchRequest]): Requests to include in the batch.
103+
104+ Returns:
105+ BatchSubmission: The submitted batch.
106+
107+ Raises:
108+ BatchError: If batch submission fails.
109+ """
93110 try :
94111 batch = self ._client .batch .create_batches (
95112 endpoint = "/v1/chat/completions" ,
@@ -111,6 +128,17 @@ def create(self, requests: Sequence[BatchRequest]) -> BatchSubmission:
111128 return _to_submission (batch )
112129
113130 def fetch (self , batch_id : str ) -> BatchSubmission :
131+ """Fetch a batch submission by its identifier.
132+
133+ Args:
134+ batch_id (str): Identifier of the batch to fetch.
135+
136+ Returns:
137+ BatchSubmission: The fetched batch submission.
138+
139+ Raises:
140+ BatchError: If the batch cannot be fetched.
141+ """
114142 try :
115143 batch = self ._client .batch .get_batches (id = batch_id )
116144 except Exception as exc :
@@ -120,6 +148,14 @@ def fetch(self, batch_id: str) -> BatchSubmission:
120148
121149@dataclass (frozen = True , slots = True )
122150class _PendingBatch :
151+ """Represent a batch awaiting completion.
152+
153+ Attributes:
154+ requests (tuple[BatchRequest, ...]): Requests included in the batch.
155+ submission (BatchSubmission): Submission associated with the batch.
156+ deadline (float): Deadline for the batch.
157+ """
158+
123159 requests : tuple [BatchRequest , ...]
124160 submission : BatchSubmission
125161 deadline : float
@@ -206,16 +242,42 @@ def run_batches(
206242
207243
208244def _chunks (requests : Sequence [BatchRequest ], size : int ) -> list [list [BatchRequest ]]:
245+ """Split requests into chunks of the specified size.
246+
247+ Args:
248+ requests (Sequence[BatchRequest]): Requests to split.
249+ size (int): Maximum number of requests per chunk.
250+
251+ Returns:
252+ list[list[BatchRequest]]: The requests grouped into chunks.
253+ """
209254 return [list (requests [start : start + size ]) for start in range (0 , len (requests ), size )]
210255
211256
212257def _is_complete (submission : BatchSubmission ) -> bool :
258+ """Check whether a batch submission is complete.
259+
260+ Args:
261+ submission (BatchSubmission): Submission to check.
262+
263+ Returns:
264+ bool: Whether the submission has a final status and, if completed, has results.
265+ """
213266 if submission .status not in FINAL_STATUSES :
214267 return False
215268 return not (submission .status == "completed" and submission .results is None )
216269
217270
218271def _collect (item : _PendingBatch , results : dict [str , BatchResult ]) -> None :
272+ """Collect results for a completed batch or record failures for an unsuccessful one.
273+
274+ Args:
275+ item (_PendingBatch): Pending batch containing the submission and its requests.
276+ results (dict[str, BatchResult]): Mapping to update with results keyed by custom ID.
277+
278+ Returns:
279+ None: Results are added to the supplied mapping.
280+ """
219281 submission = item .submission
220282 if submission .status != "completed" :
221283 detail = f": { submission .error } " if submission .error else ""
@@ -242,13 +304,30 @@ def _failures(
242304 requests : Sequence [BatchRequest ],
243305 message : str ,
244306) -> dict [str , BatchResult ]:
307+ """Build an error result for each request.
308+
309+ Args:
310+ requests (Sequence[BatchRequest]): Requests to create error results for.
311+ message (str): Error message to assign to each result.
312+
313+ Returns:
314+ dict[str, BatchResult]: Error results keyed by request custom ID.
315+ """
245316 return {
246317 request .custom_id : BatchResult (custom_id = request .custom_id , error = message )
247318 for request in requests
248319 }
249320
250321
251322def _to_submission (batch : BatchObject ) -> BatchSubmission :
323+ """Convert a batch object to a submission.
324+
325+ Args:
326+ batch (BatchObject): Batch object to convert.
327+
328+ Returns:
329+ BatchSubmission: Submission containing the batch ID and status, converted results if present, and the error message if present.
330+ """
252331 results = None
253332 if batch .results is not None :
254333 results = tuple (_to_result (result ) for result in batch .results )
@@ -257,12 +336,28 @@ def _to_submission(batch: BatchObject) -> BatchSubmission:
257336
258337
259338def _to_result (result : Result ) -> BatchResult :
339+ """Convert a result to a batch result.
340+
341+ Args:
342+ result (Result): The result to convert.
343+
344+ Returns:
345+ BatchResult: A result containing the error message if an error is present, or the chat content otherwise.
346+ """
260347 if result .error is not None :
261348 return BatchResult (custom_id = result .custom_id , error = result .error .message )
262349 return BatchResult (custom_id = result .custom_id , content = _chat_content (result ))
263350
264351
265352def _chat_content (result : Result ) -> str | None :
353+ """Extract the first nonempty chat completion message.
354+
355+ Args:
356+ result (Result): The result containing a response to inspect.
357+
358+ Returns:
359+ str | None: The first nonempty message content, or `None` if no chat completion content is available.
360+ """
266361 if result .response is None :
267362 return None
268363 body = result .response .body
0 commit comments