Skip to content

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

@chedkowski-leonsoftware

Description

Hi, BatchesService::results() has incorrect return type causing PHP runtime error:

Anthropic\Core\Services\Beta\Messages\BatchesService::results(): Return value must be of type 
Anthropic\Beta\Messages\Batches\MessageBatchIndividualResponse, 
Generator returned in 
/home/leon/public_html/vendor/anthropic-ai/sdk/src/Core/Services/Beta/Messages/BatchesService.php(272)

Declared return type: MessageBatchIndividualResponse
Actual return type: Generator<MessageBatchIndividualResponse>

Environment:

  • PHP: 8.4+
  • SDK version: 0.3.0 (0.4.0 not released due to failing "Create releases / release (push)" job)
  • declare(strict_types=1) enabled

Workaround

Use client->request() directly to bypass the buggy method:

public function retrieveBatchResult(AnthropicBatchId $batch_id): MessageBatchIndividualResponse
{
	$generator = $this->client->request(
		method: 'get',
		path: sprintf('/v1/messages/batches/%s/results', $batch_id->value),
		headers: [
			'Accept' => 'application/x-jsonl',
			'anthropic-beta' => 'message-batches-2024-09-24',
		],
	);

	$complete_content = implode('', iterator_to_array($generator));
	$decoded = json_decode($complete_content, true, 512, JSON_THROW_ON_ERROR);

	return Conversion::coerce(MessageBatchIndividualResponse::class, value: $decoded);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions