Feature - Add support to batching and files API's for Anthropic and OpenAI#951
Open
joao-salomao wants to merge 12 commits intoprism-php:mainfrom
Open
Feature - Add support to batching and files API's for Anthropic and OpenAI#951joao-salomao wants to merge 12 commits intoprism-php:mainfrom
joao-salomao wants to merge 12 commits intoprism-php:mainfrom
Conversation
…ob management and result handling
…h job management, result handling, and error mapping
…-anthropic-and-openai
…lue for data retrieval
… provider methods for batch management
…delete, and metadata retrieval functionalities
…e batch job handling with inputFileId support
…xtRequest
- Use ?? [] on items to avoid passing null to buildAndUploadFile()
- Cast json_encode() result to string to satisfy non-empty-string return type
- Change clientRetry default from [] to [0] to satisfy array{0: int} type constraint
Made-with: Cursor
…s from OpenAI responses
…update tests for empty array responses
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.
Description
Adds a Files API and Batch Processing API for Anthropic and OpenAI providers, enabling asynchronous processing of large volumes of LLM requests at reduced cost.
Files API (
src/Files/)Shared foundation:
FileData/FileListResult/DeleteFileResult— normalized response DTOsUploadFileRequest,DownloadFileRequest,DeleteFileRequest,ListFilesRequest,GetFileMetadataRequest— typed request value objectsFluent builder (
Prism::files()):upload(content, filename, mimeType?)— multipart file uploadlist(limit?, afterId?, beforeId?)— cursor-paginated listinggetMetadata(fileId)— fetch file metadatadownload(fileId)— download raw file content as a stringdelete(fileId)— delete a fileProvider implementations:
Providers/{Provider}/Handlers/Files/HandlesFileResponseconcerns per providerbeforeIdis Anthropic-only;purposefilter (viawithProviderOptions) is OpenAI-onlypurposefield on upload (batch,assistants,fine-tune, etc.)Provider contract (
Provider.php):uploadFile(),listFiles(),getFileMetadata(),downloadFile(),deleteFile()— default to throwingunsupportedProviderActionBatch Processing API (
src/Batch/)Shared foundation:
BatchRequest/BatchRequestItem— input DTOs;BatchRequestItemwraps aTextRequestwith acustomIdBatchJob/BatchJobRequestCounts— normalized job representation across providers, includinginputFileId,outputFileId,errorFileId, timestamps, and statusBatchListResult— paginated listing responseBatchResultItem— individual result with text, usage, and error infoBatchStatus/BatchResultStatus— enums normalizing provider-specific statusesGetBatchResultsRequest,RetrieveBatchRequest,ListBatchesRequest,CancelBatchRequest— typed request value objectsFluent builder (
Prism::batch()):create(items?, inputFileId?)— create a batchretrieve(batchId)— poll for statuslist(limit?, afterId?, beforeId?)— cursor-paginated listinggetResults(batchId)— retrieve all results asBatchResultItem[]cancel(batchId)— cancel an in-progress batchProvider contract (
Provider.php):batch(),retrieveBatch(),listBatches(),getBatchResults(),cancelBatch()— default to throwingunsupportedProviderActionAnthropic implementation:
Create,Retrieve,ListBatches,Results,CancelHandlesBatchResponseandMapsBatchResultsconcerns for shared mappingBatchResultItem[])OpenAI implementation:
HandlesBatchResponseandMapsBatchResultsconcernsBatchRequestItem[]directly — Prism serializes them to JSONL usingBuildsRequestBody::buildHttpRequestPayload()and auto-uploads via the Files API; no manual file handling requiredinputFileIddirectly as an escape hatchitemsandinputFileIdthrows aPrismExceptionResultshandler uses injectedretrieveBatchanddownloadFileclosures for testable coordinationBuildsRequestBodyconcern extracted from text/stream/batch handlers, eliminating duplicated request body mappingFluent Builder — shared traits
Both
Prism::files()andPrism::batch()use the same traits as the existing text/structured/embeddings builders:ConfiguresProviders—using(),whenProvider()ConfiguresClient—withClientOptions(),withClientRetry()HasProviderOptions—withProviderOptions()TextRequestergonomicsAll constructor parameters except
modelnow have sensible defaults, making it straightforward to construct aTextRequestdirectly for batch use without needing the full fluent builder chain:Documentation
docs/core-concepts/files.md— full Files API reference including upload, list (with provider support table), metadata, download, delete, pagination, and provider-conditional optionsdocs/core-concepts/batch.md— full Batch API reference including create-poll-retrieve workflow, provider-specific notes, status table, result item properties, listing, cancellation, and links to the official Anthropic and OpenAI batch docsTests
CreateTest,RetrieveTest,ListTest,ResultsTest,CancelTestResultshandlers on both providers:ResultsUnitTest— mocks HTTP/callbacks, covering succeeded/errored/expired/canceled parsing, blank-line skipping, and null output file ID handlingBreaking Changes
None. All new methods on
Providerdefault to throwingunsupportedProviderAction, so existing custom providers are unaffected. The only structural change to an existing class isTextRequest, where constructor parameters now have defaults — all existing call sites using named arguments or the fluent builder are unaffected.