Skip to main content

Overview

Instructor has a built-in support for dynamically constructing JSON Schema using JsonSchema class. It is useful when you want to shape the structures during runtime.

Example

<?php
require 'examples/boot.php';

use Cognesy\Http\Creation\HttpClientBuilder;
use Cognesy\Instructor\StructuredOutput;
use Cognesy\Instructor\StructuredOutputRuntime;
use Cognesy\Utils\JsonSchema\JsonSchema;

$schema = JsonSchema::object(
    properties: [
        JsonSchema::string('name', 'User name'),
        JsonSchema::integer('age', 'User age'),
    ],
    requiredProperties: ['name', 'age'],
);

$debugHttpClient = (new HttpClientBuilder)->withDebugConfig(ExampleConfig::debugPreset('on'))->create();

$user = (new StructuredOutput(
    StructuredOutputRuntime::fromDefaults(httpClient: $debugHttpClient)
))
    ->withMessages("Jason is 25 years old and works as an engineer")
    ->withResponseJsonSchema($schema)
    ->withDeserializers()
    ->withDefaultToStdClass()
    ->get();

dump($user);

assert(gettype($user) === 'object');
assert(get_class($user) === 'stdClass');
assert(isset($user->name));
assert(isset($user->age));
assert($user->name === 'Jason');
assert($user->age === 25);

?>