Feat: Add Batch API support #401
Open
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.
What
Add full support for OpenAI's Batch API, enabling asynchronous processing of large request volumes with 50% cost savings.
Core API methods:
createBatch- Create a batch from an uploaded JSONL fileretrieveBatch- Get batch status and detailslistBatches- List batches with paginationcancelBatch- Cancel an in-progress batchFile API additions:
retrieveFileContent- Download file content (needed for batch output)deleteFile- Delete uploaded filesConvenience methods:
submitBatch- Handles JSONL encoding, file upload, and batch creation in one callwaitForBatch- Polls for completion and returns parsed responsesNew types:
BatchQuery,BatchResult,BatchListResult,BatchResponseLineBatchEndpoint,BatchCompletionWindow,BatchStatusenumsBatchErrorfor convenience method error handlingFileDeleteResultfor file deletion responsesWhy
The Batch API offers significant benefits for processing large volumes of requests:
This is particularly useful for bulk data processing, evaluations, benchmarks, and batch content generation.
Affected Areas
Sources/OpenAI/Public/Models/- New Batch API modelsSources/OpenAI/Public/Protocols/- Protocol extensionsSources/OpenAI/OpenAI.swift- Closure-based implementations + API pathsSources/OpenAI/OpenAI+OpenAIAsync.swift- Async implementations + convenience methodsSources/OpenAI/Private/- Request builders and raw data handlingTests/OpenAITests/- Unit and integration testsREADME.md- DocumentationMore Info
BatchAPITests.swift) and integration tests (BatchAPIIntegrationTests.swift)