Instructor for PHP

Instructor for PHP is a lightweight library that makes it easy to get structured outputs from Large Language Models (LLMs). Built on top of modern PHP 8.2+ features, it provides a simple, type-safe way to work with AI models.

Key Features

  • Type Safety: Full PHP 8.2+ type system support with strict typing
  • Multiple LLM Support: Works with OpenAI, Anthropic, Gemini, Cohere, and more
  • Validation: Built-in validation with custom rules and LLM-powered validation
  • Streaming: Real-time partial object updates for better UX
  • Function Calling: Native support for LLM function/tool calling
  • Zero Dependencies: Clean, lightweight implementation

Quick Example

<?php
use Cognesy\Instructor\StructuredOutput;

class Person {
    public string $name;
    public int $age;
    public string $occupation;
}

$text = "Extract: Jason is 25 years old and works as a software engineer.";

$person = (new StructuredOutput)
    ->withResponseClass(Person::class)
    ->withMessages($text)
    ->get();

echo $person->name; // "Jason"
echo $person->age;  // 25
echo $person->occupation; // "software engineer"

Getting Started

Choose your path:

Architecture

This project consists of several modular packages:

Community


Instructor for PHP - Making AI outputs predictable and type-safe.