Add batch API endpoints for asynchronous LLM processing#46
Open
Add batch API endpoints for asynchronous LLM processing#46
Conversation
Implement five /v1/batches endpoints (create, retrieve, cancel, list, results) that proxy batch operations through the any-llm SDK. Includes JSONL temp file construction for batch creation, provider support validation (SUPPORTS_BATCH), 409 handling for incomplete batch results, usage logging, and automatic temp file cleanup. Registered in standalone mode only, consistent with other LLM proxy endpoints. Regenerated OpenAPI spec.
…gging, error handling, and missing tests Address review feedback for the batch API implementation: - Add FIXME/docstring documenting that retrieve_batch_results uses aretrieve_batch as workaround (SDK lacks aretrieve_batch_results) - Add db parameter to log_batch_usage for spec consistency - Add usage logging to retrieve_batch_results endpoint - Handle ValueError from split_model_provider for invalid model format (400) - Replace deprecated HTTP_422_UNPROCESSABLE_ENTITY with HTTP_422_UNPROCESSABLE_CONTENT - Move Batch import under TYPE_CHECKING - Add test_create_batch_invalid_model_format integration test - Add test_retrieve_batch_results_logs_usage integration test - Regenerate OpenAPI spec
Import aretrieve_batch_results, BatchNotCompleteError, and Batch directly instead of guarding behind TYPE_CHECKING. Also import BackgroundTasks and switch get_db to get_db_if_needed in preparation for subsequent fixes.
The db parameter was accepted but never used. Remove it from the function signature and all call sites. Also replace model='batch' with empty string in results logging to avoid polluting usage data.
Use get_db_if_needed with AsyncSession | None for consistency with chat.py pattern. Add BackgroundTasks to create_batch signature per spec.
…oint Replace workaround that called aretrieve_batch and returned batch metadata with proper aretrieve_batch_results call that returns per-request results. Catch BatchNotCompleteError for 409 response. Serialize BatchResult items matching the spec response shape.
Isolate test from SDK behavior by mocking split_model_provider to raise ValueError directly instead of relying on SDK internals.
Mock aretrieve_batch_results instead of aretrieve_batch. Verify per-request results response shape with BatchResult, BatchResultItem, and BatchResultError. Use BatchNotCompleteError for 409 test.
Add uv source override to use the local SDK which includes aretrieve_batch_results, BatchResult, BatchResultItem, BatchResultError, and BatchNotCompleteError.
Update the batch results endpoint description to remove the obsolete workaround note now that the SDK ships batch result types.
CI fails because uv.lock references ../any-llm which doesn't exist in CI. Switch to git branch source so CI can resolve the dependency.
…attr-defined check
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
/v1/batches(create, retrieve, cancel, list, results) that proxy batch operations to LLM providers through the any-llm SDKSUPPORTS_BATCH), usage logging, JSONL temp file construction with cleanup, andBatchNotCompleteError→ 409 mappingEndpoints
POST/v1/batchesGET/v1/batches/{batch_id}?provider=POST/v1/batches/{batch_id}/cancel?provider=GET/v1/batches?provider=GET/v1/batches/{batch_id}/results?provider=Files Changed
src/gateway/api/routes/batches.py— New route module with all five handlers, Pydantic models, usage loggingsrc/gateway/api/main.py— Router registration (standalone mode only)tests/unit/test_batches_route.py— Pydantic validation teststests/integration/test_batches_endpoint.py— Full integration test suite with mocked SDKdocs/public/openapi.json— Regenerated OpenAPI specpyproject.toml— SDK dependency pinned to local source with batch types