PHPackages                             shanginn/openai-sdk - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. shanginn/openai-sdk

ActiveLibrary[API Development](/categories/api)

shanginn/openai-sdk
===================

1.4.1(4mo ago)089MITPHPPHP &gt;=8.2CI passing

Since Aug 16Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/shanginn/openai-sdk-php)[ Packagist](https://packagist.org/packages/shanginn/openai-sdk)[ RSS](/packages/shanginn-openai-sdk/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (9)Dependencies (10)Versions (12)Used By (0)

OpenAI SDK PHP
==============

[](#openai-sdk-php)

[![Latest Stable Version](https://camo.githubusercontent.com/8539eba2606003c8731c50439803722d7629b92a0185ee9a59d98f4ae0ab61a4/68747470733a2f2f706f7365722e707567782e6f72672f7368616e67696e6e2f6f70656e61692d73646b2d7068702f76)](https://packagist.org/packages/shanginn/openai-sdk-php) [![Total Downloads](https://camo.githubusercontent.com/1bb6de9ffbedb143da682c5055850c84d39eff135ea9825848d67db37befa60b/68747470733a2f2f706f7365722e707567782e6f72672f7368616e67696e6e2f6f70656e61692d73646b2d7068702f646f776e6c6f616473)](https://packagist.org/packages/shanginn/openai-sdk-php)[![License](https://camo.githubusercontent.com/16eb269ea1a92f5154adf382f533b5023ac09f0df45dcd6623744eed40571de8/68747470733a2f2f706f7365722e707567782e6f72672f7368616e67696e6e2f6f70656e61692d73646b2d7068702f6c6963656e7365)](https://packagist.org/packages/shanginn/openai-sdk-php) [![Build Status](https://github.com/shanginn/openai-sdk-php/actions/workflows/ci.yml/badge.svg)](https://github.com/shanginn/openai-sdk-php/actions/workflows/ci.yml) [![Coverage Status](https://camo.githubusercontent.com/6bf2fc12b4205fdae43d89e29551e23cb776ccd6c0fb2a97a016befa321fcca7/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f7368616e67696e6e2f6f70656e61692d73646b2d7068702f62616467652e7376673f6272616e63683d6d61696e)](https://coveralls.io/github/shanginn/openai-sdk-php?branch=main)

A modern, strongly-typed PHP SDK for interacting with the OpenAI API, focusing initially on the Chat Completions endpoint. Built with asynchronous capabilities in mind using `amphp/http-client` and robust serialization/deserialization via `crell/serde`.

Features
--------

[](#features)

- Access to the OpenAI Chat Completions API (`/v1/chat/completions`).
- **Strongly-Typed Objects:** Uses PHP classes for Requests, Responses, Messages, Tools, and Schemas, providing better IDE autocompletion and type safety.
- **Tool Calling:** Define and use tools (functions) that the OpenAI models can invoke. Includes automatic deserialization of tool arguments into PHP objects based on class definitions with attributes like `Spiral\JsonSchemaGenerator\Attribute\Field`.
- **JSON Schema Mode:** Force the model to output JSON conforming to a specific structure defined by your PHP classes implementing `JsonSchemaInterface`, utilizing attributes like `Spiral\JsonSchemaGenerator\Attribute\Field` for detailed schema generation.
- **Image Input:** Supports sending images along with text prompts using `UserMessage` and `ImageContentPart` (compatible with models like GPT-4o).
- **Asynchronous Client:** Leverages `amphp/http-client` for non-blocking I/O (though the current `OpenaiClient` implementation buffers the full response).
- **Serialization:** Uses `crell/serde` and `symfony/serializer` for mapping between PHP objects and OpenAI's JSON format.
- **Simplified Wrapper:** Includes an `OpenaiSimple` class for common use cases like simple text generation, JSON object generation, and tool calling with less boilerplate.
- **Custom Exceptions:** Provides specific exceptions for different API error conditions (e.g., `OpenaiErrorResponseException`, `OpenaiNoChoicesException`, `OpenaiWrongSchemaException`).

Installation
------------

[](#installation)

Install the package via Composer:

```
composer require shanginn/openai-sdk
```

Usage
-----

[](#usage)

### Simple Text Generation (`OpenaiSimple`)

[](#simple-text-generation-openaisimple)

This is the easiest way to get a text response for a simple prompt.

```
