Skip to content

fix: BatchesService::results() crashes — missing stream parameter#15

Open
clowerweb wants to merge 1 commit intoanthropics:mainfrom
clowerweb:fix/batches-results-return-type
Open

fix: BatchesService::results() crashes — missing stream parameter#15
clowerweb wants to merge 1 commit intoanthropics:mainfrom
clowerweb:fix/batches-results-return-type

Conversation

@clowerweb
Copy link

Summary

Fixes #8

  • BatchesRawService::results() was missing stream: JsonLStream::class in its $this->client->request() call, causing RawResponse::parse() to try to coerce a Generator into a single MessageBatchIndividualResponse model (runtime crash)
  • Updated return types from MessageBatchIndividualResponse to BaseStream across contracts and services to match the actual JSONL streamed response shape
  • Applied the same fix to both the non-beta (Messages) and beta (Beta\Messages) namespaces

Root Cause

The /v1/messages/batches/{id}/results endpoint returns application/x-jsonl (multiple JSON objects, one per line). The sibling resultsStream() method correctly passes stream: JsonLStream::class to $this->client->request(), but the results() method was missing this parameter.

This appears to be a Stainless codegen template bug: the JSONL endpoint handler generates the stream: parameter for the *Stream() variant but omits it for the non-stream results() variant. Both methods hit the same JSONL endpoint and need the stream class to parse correctly. This should be fixed in the codegen templates upstream so it doesn't regress on the next generation pass.

Note: PR #13 (v0.6.0) does not fix this issue.

Files Changed (8)

File Change
src/Services/Messages/BatchesRawService.php Added stream: JsonLStream::class to results()
src/Services/Messages/BatchesService.php Return type → BaseStream, added @return phpdoc
src/ServiceContracts/Messages/BatchesContract.php Return type → BaseStream, added @return phpdoc
src/ServiceContracts/Messages/BatchesRawContract.php Updated @return phpdoc to BaseResponse<BaseStream<…>>
src/Services/Beta/Messages/BatchesRawService.php Added stream: JsonLStream::class to results()
src/Services/Beta/Messages/BatchesService.php Return type → BaseStream, added @return phpdoc
src/ServiceContracts/Beta/Messages/BatchesContract.php Return type → BaseStream, added @return phpdoc
src/ServiceContracts/Beta/Messages/BatchesRawContract.php Updated @return phpdoc to BaseResponse<BaseStream<…>>

Test Plan

  • composer run test — existing tests pass
  • composer run lint (PHPStan) — no type errors
  • Verify results() now returns a BaseStream that can be iterated for MessageBatchIndividualResponse objects, matching resultsStream() behavior

🤖 Generated with Claude Code

The `/v1/messages/batches/{id}/results` endpoint returns
`application/x-jsonl` (multiple JSON objects). The `results()` method
in both `BatchesRawService` (Messages and Beta\Messages) was missing
the `stream: JsonLStream::class` parameter in the `$this->client->request()`
call, while the sibling `resultsStream()` method correctly included it.

Without this parameter, `RawResponse::parse()` attempts to coerce a
Generator into a single `MessageBatchIndividualResponse` model, causing
a runtime crash.

This also updates return types from `MessageBatchIndividualResponse` to
`BaseStream` across contracts and services to match the actual streamed
response shape.

Fixes anthropics#8

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@AtlisTechnologies
Copy link

Yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BatchesService::results() - incorrect return type causing PHP runtime error

2 participants