aisdk / openai-compatible
Shared OpenAI-compatible chat-completions wire format (request builder, response/stream parsers) for the PHP AI SDK.
Requires
- php: ^8.3
- aisdk/core: ^0.1
Requires (Dev)
- laravel/pint: ^1.18
- nyholm/psr7: ^1.8
- pestphp/pest: ^3.0 || ^4.0
- phpstan/phpstan: ^2.0
- rector/rector: ^2.0
This package is auto-updated.
Last update: 2026-06-30 05:42:02 UTC
README
Shared OpenAI-compatible chat-completions wire adapter for the PHP AI SDK. Reusable by any provider that speaks the OpenAI /chat/completions REST/SSE protocol.
Installation
composer require aisdk/openai-compatible
What This Package Does
This package provides the shared wire-format bridge between core portable contracts (AiSdk\*) and the OpenAI-compatible chat-completions shape. It is used by providers like Groq, xAI, OpenRouter, and others that implement the OpenAI-compatible API.
It owns:
- Request body building (
ChatRequestBuilder) - Response parsing (
ChatResponseParser) - SSE stream parsing (
ChatStreamParser) - Message conversion (
ChatMessageConverter) - Tool conversion (
ChatToolConverter) - Usage normalization (
ChatUsage) - Finish reason mapping (
MapsFinishReason)
It does not own:
- Provider authentication
- Model catalogs
- Provider-specific quirks or fallback behavior
Usage
This package is consumed by provider packages, not directly by end users. A provider that speaks OpenAI-compatible chat completions uses it like this:
use AiSdk\OpenAICompatible\ChatRequestBuilder; use AiSdk\OpenAICompatible\ChatResponseParser; use AiSdk\OpenAICompatible\ChatStreamParser; $body = ChatRequestBuilder::build($modelId, $providerName, $request, stream: false); $payload = $this->runner()->postJson($url, $body, $headers, $providerName); $response = ChatResponseParser::parse($payload, $providerName);
Provider Integration
To build a provider on top of this package:
- Depend on
aisdk/coreandaisdk/openai-compatible. - Create a provider class extending
BaseProvider. - Create a text model extending
BaseModelthat callsChatRequestBuilder::build(),ChatResponseParser::parse(), andChatStreamParser::parse(). - Add provider-specific auth, base URL, headers, and model catalog.
- Apply any provider-specific adaptations (e.g., structured output downgrades) after calling
ChatRequestBuilder::build().
Testing
composer test