@anvia/openai ​
OpenAI’s provider adapter covers the broadest set of Anvia model contracts: completions, embeddings, image generation, speech generation, transcription, and model listing. Use it at the model boundary while keeping agents and application logic provider-independent.
| Support | First-party |
| Version | 0.5.1 |
| Runtime | ESM, server-side JavaScript |
| Peer | @anvia/core >=0.7.1 <1.0.0 |
Install ​
pnpm add @anvia/openai @anvia/coreCreate a completion model ​
import { AgentBuilder } from '@anvia/core'
import { OpenAIClient } from '@anvia/openai'
const openai = new OpenAIClient({
apiKey: process.env.OPENAI_API_KEY,
})
const agent = new AgentBuilder(
'assistant',
openai.completionModel('gpt-5'),
)
.instructions('Answer clearly and concisely.')
.build()
const result = await agent.prompt('Explain semantic search in one paragraph.').send()
console.log(result.output)The client uses OpenAI’s Responses API by default. A custom baseUrl selects the Chat Completions adapter by default, which is useful for OpenAI-compatible services. Set completionApi explicitly when the endpoint supports both.
Capabilities ​
| Capability | Factory | Default |
|---|---|---|
| Streaming completion | completionModel() | gpt-5 |
| Dense embeddings | embeddingModel() | text-embedding-3-small |
| Image generation | imageGenerationModel() | gpt-image-1 |
| Text-to-speech | audioGenerationModel() | tts-1 |
| Transcription | transcriptionModel() | whisper-1 |
| Model inventory | listModels() | Provider model list |
Both completion adapters normalize messages, tool calls, reasoning, usage, structured output, and streaming events into Anvia contracts. The Responses adapter is the native OpenAI path; the Chat adapter supports OpenAI-compatible endpoints and preserves provider-specific reasoning history when required.
Common patterns ​
Configure an OpenAI-compatible endpoint ​
const compatible = new OpenAIClient({
apiKey: process.env.PROVIDER_API_KEY,
baseUrl: 'https://provider.example.com/v1',
completionApi: 'chat',
})
const model = compatible.completionModel('provider/model-name')Reuse one client across model capabilities ​
const completion = openai.completionModel('gpt-5')
const embeddings = openai.embeddingModel('text-embedding-3-small', {
dimensions: 1536,
maxBatchSize: 64,
})
const images = openai.imageGenerationModel()Create the provider client once at the server boundary. Keep credentials there, inject the returned model contracts, and keep fallback policy and tenant routing in application code.
Compatibility ​
@anvia/openai is an ESM package and uses the official openai SDK. It accepts a preconfigured SDK client for custom transports. Media and embedding methods require endpoints that implement the corresponding OpenAI APIs; an OpenAI-compatible chat endpoint does not imply support for those other capabilities.