From 756fba0b1dad050a3140450e805832566dd22ff0 Mon Sep 17 00:00:00 2001 From: TJ Miller Date: Sun, 15 Dec 2024 16:38:36 -0500 Subject: [PATCH] Rename ->object to ->structured (#103) --- docs/core-concepts/structured-output.md | 8 ++++---- src/Structured/Response.php | 4 ++-- src/Structured/ResponseBuilder.php | 2 +- tests/Generators/StructuredGeneratorTest.php | 4 ++-- tests/Providers/Anthropic/StructuredTest.php | 4 ++-- tests/Providers/Ollama/StructuredTest.php | 4 ++-- tests/Providers/OpenAI/StructuredTest.php | 8 ++++---- 7 files changed, 17 insertions(+), 17 deletions(-) diff --git a/docs/core-concepts/structured-output.md b/docs/core-concepts/structured-output.md index 4505383..d5d2b7e 100644 --- a/docs/core-concepts/structured-output.md +++ b/docs/core-concepts/structured-output.md @@ -30,7 +30,7 @@ $response = Prism::structured() ->generate(); // Access your structured data -$review = $response->object; +$review = $response->structured; echo $review['title']; // "Inception" echo $review['rating']; // "5 stars" echo $review['summary']; // "A mind-bending..." @@ -81,7 +81,7 @@ $response = Prism::structured() ->generate(); // Access the structured data as a PHP array -$data = $response->object; +$data = $response->structured; // Get the raw response text if needed echo $response->text; @@ -100,11 +100,11 @@ $rawResponse = $response->response; > [!TIP] > Always validate the structured data before using it in your application: ```php -if ($response->object === null) { +if ($response->structured === null) { // Handle parsing failure } -if (!isset($response->object['required_field'])) { +if (!isset($response->structured['required_field'])) { // Handle missing required data } ``` diff --git a/src/Structured/Response.php b/src/Structured/Response.php index 98afdf8..53e56ab 100644 --- a/src/Structured/Response.php +++ b/src/Structured/Response.php @@ -16,7 +16,7 @@ class Response /** * @param Collection $steps * @param Collection $responseMessages - * @param array $object + * @param array $structured * @param ToolCall[] $toolCalls * @param ToolResult[] $toolResults * @param array{id: string, model: string} $response @@ -25,7 +25,7 @@ public function __construct( public readonly Collection $steps, public readonly Collection $responseMessages, public readonly string $text, - public readonly ?array $object, + public readonly ?array $structured, public readonly FinishReason $finishReason, public readonly array $toolCalls, public readonly array $toolResults, diff --git a/src/Structured/ResponseBuilder.php b/src/Structured/ResponseBuilder.php index 1de33ae..86fb89d 100644 --- a/src/Structured/ResponseBuilder.php +++ b/src/Structured/ResponseBuilder.php @@ -47,7 +47,7 @@ public function toResponse(): Response steps: $this->steps, responseMessages: $this->responseMessages, text: $finalStep->text, - object: $finalStep->finishReason === FinishReason::Stop + structured: $finalStep->finishReason === FinishReason::Stop ? $this->decodeObject($finalStep->text) : [], finishReason: $finalStep->finishReason, diff --git a/tests/Generators/StructuredGeneratorTest.php b/tests/Generators/StructuredGeneratorTest.php index 767b8ac..91d18f7 100644 --- a/tests/Generators/StructuredGeneratorTest.php +++ b/tests/Generators/StructuredGeneratorTest.php @@ -196,7 +196,7 @@ ->generate(); // Assert response - expect($response->object)->toBe(['forecast_summary' => '70° and sunny']); + expect($response->structured)->toBe(['forecast_summary' => '70° and sunny']); expect($response->finishReason)->toBe(FinishReason::Stop); expect($response->toolCalls)->toBeEmpty(); expect($response->toolResults)->toBeEmpty(); @@ -334,7 +334,7 @@ ->generate(); // Assert Response - expect($response->object)->toBe(['forecast_summary' => 'The weather is 75 and sunny!']); + expect($response->structured)->toBe(['forecast_summary' => 'The weather is 75 and sunny!']); expect($response->finishReason)->toBe(FinishReason::Stop); // Assert steps diff --git a/tests/Providers/Anthropic/StructuredTest.php b/tests/Providers/Anthropic/StructuredTest.php index da1c910..6e4ce22 100644 --- a/tests/Providers/Anthropic/StructuredTest.php +++ b/tests/Providers/Anthropic/StructuredTest.php @@ -45,8 +45,8 @@ ->withPrompt('What time is the tigers game today and should I wear a coat?') ->generate(); - expect($response->object)->toBeArray(); - expect($response->object)->toBe([ + expect($response->structured)->toBeArray(); + expect($response->structured)->toBe([ 'weather' => '90° and sunny', 'game_time' => '3pm', 'coat_required' => false, diff --git a/tests/Providers/Ollama/StructuredTest.php b/tests/Providers/Ollama/StructuredTest.php index 7a63575..ddf48da 100644 --- a/tests/Providers/Ollama/StructuredTest.php +++ b/tests/Providers/Ollama/StructuredTest.php @@ -45,8 +45,8 @@ ->withPrompt('What time is the tigers game today and should I wear a coat?') ->generate(); - expect($response->object)->toBeArray(); - expect($response->object)->toBe([ + expect($response->structured)->toBeArray(); + expect($response->structured)->toBe([ 'weather' => 'sunny, 90°', 'game_time' => '3pm', 'coat_required' => false, diff --git a/tests/Providers/OpenAI/StructuredTest.php b/tests/Providers/OpenAI/StructuredTest.php index a9f6bb0..ea938cd 100644 --- a/tests/Providers/OpenAI/StructuredTest.php +++ b/tests/Providers/OpenAI/StructuredTest.php @@ -48,8 +48,8 @@ ->withPrompt('What time is the tigers game today and should I wear a coat?') ->generate(); - expect($response->object)->toBeArray(); - expect($response->object)->toBe([ + expect($response->structured)->toBeArray(); + expect($response->structured)->toBe([ 'weather' => 'The weather will be 90° and sunny', 'game_time' => 'The Tigers game is at 3 pm in Detroit', 'coat_required' => false, @@ -89,8 +89,8 @@ ->withPrompt('What time is the tigers game today and should I wear a coat?') ->generate(); - expect($response->object)->toBeArray(); - expect($response->object)->toBe([ + expect($response->structured)->toBeArray(); + expect($response->structured)->toBe([ 'weather' => 'The weather will be 90° and sunny', 'game_time' => 'The tigers game is at 3pm in Detroit', 'coat_required' => false,