OpenAiClient.ts
OpenAiClient.ts overview
Section titled “OpenAiClient.ts overview”The OpenAiClient module defines the low-level Effect service used by the
OpenAI integration for Responses API and embedding requests. It builds a
configured HTTP client with authentication and OpenAI organization or project
headers, exposes helpers for non-streaming responses, SSE response streams,
WebSocket response streams, and embeddings, and maps transport or decoding
failures into AiError.
Since v4.0.0
Exports Grouped by Category
Section titled “Exports Grouped by Category”Events
Section titled “Events”ResponseStreamEvent (type alias)
Section titled “ResponseStreamEvent (type alias)”Response stream event emitted by the OpenAI Responses API.
Signature
type ResponseStreamEvent = typeof OpenAiSchema.ResponseStreamEvent.TypeSince v4.0.0
Websocket mode
Section titled “Websocket mode”OpenAiSocket (class)
Section titled “OpenAiSocket (class)”Service for creating OpenAI response streams over a WebSocket connection.
When to use
Use when you need direct access to the WebSocket-backed response streaming service rather than wrapping an effect with WebSocket mode.
Details
createResponseStream sends a response.create message over the WebSocket
connection and returns an HTTP response together with a stream of
ResponseStreamEvent values.
Gotchas
WebSocket response streams are serialized to one request at a time by the shared socket service.
See
withWebSocketModefor enabling WebSocket mode for one effectlayerWebSocketModefor providing WebSocket mode through a layer
Signature
declare class OpenAiSocketSince v4.0.0
layerWebSocketMode
Section titled “layerWebSocketMode”Uses OpenAI’s websocket mode for all responses that use the Layer.
When to use
Use to provide WebSocket mode through layer composition for effects that use OpenAI response streaming.
Gotchas
This only works with the following WebSocket constructor layers:
NodeSocket.layerWebSocketConstructorWSBunSocket.layerWebSocketConstructor
These constructor layers support the non-standard options needed to set the Authorization header.
See
withWebSocketModefor enabling WebSocket mode around a single effect
Signature
declare const layerWebSocketMode: Layer.Layer< OpenAiSocket | ResponseIdTracker.ResponseIdTracker, never, OpenAiClient | Socket.WebSocketConstructor>Since v4.0.0
withWebSocketMode
Section titled “withWebSocketMode”Uses OpenAI’s WebSocket mode for response streams within the provided effect.
When to use
Use to enable WebSocket mode around one effect that creates OpenAI response streams.
Gotchas
This only works with the following WebSocket constructor layers:
NodeSocket.layerWebSocketConstructorWSBunSocket.layerWebSocketConstructor
These constructor layers support the non-standard options needed to set the Authorization header.
See
layerWebSocketModefor providing WebSocket mode through a layerOpenAiSocketfor direct access to the WebSocket-backed streaming service
Signature
declare const withWebSocketMode: <A, E, R>( effect: Effect.Effect<A, E, R>) => Effect.Effect< A, E, Exclude<R, OpenAiSocket | ResponseIdTracker.ResponseIdTracker> | OpenAiClient | Socket.WebSocketConstructor>Since v4.0.0
constructors
Section titled “constructors”Creates an OpenAI client service with the given options.
When to use
Use when you need the OpenAI client service value inside an effect.
Details
The returned service uses the current HttpClient, prepends apiUrl or
https://api.openai.com/v1, adds the bearer token and optional OpenAI
organization/project headers, accepts JSON responses, filters for successful
HTTP statuses, and applies transformClient when provided.
Gotchas
A scoped OpenAiConfig.withClientTransform is applied when request helpers
run, after the transformClient option supplied to make.
See
layerfor providing this client from explicit optionslayerConfigfor loading client settings fromConfig
Signature
declare const make: (options: Options) => Effect.Effect<Service, never, HttpClient.HttpClient>Since v4.0.0
layers
Section titled “layers”Creates a layer for the OpenAI client with the given options.
When to use
Use when you already have explicit Options values, such as an API key or
custom API URL, and want to provide OpenAiClient as a Layer.
See
makefor constructing the client service effectfullylayerConfigfor loading client settings fromConfig
Signature
declare const layer: (options: Options) => Layer.Layer<OpenAiClient, never, HttpClient.HttpClient>Since v4.0.0
layerConfig
Section titled “layerConfig”Creates a layer for the OpenAI client from provided Config values.
When to use
Use when you need client settings for OpenAI-compatible APIs to be read from
Effect Config values while providing OpenAiClient as a Layer.
Details
Only config values supplied in options are loaded. Omitted fields are
passed to make as undefined, and transformClient is forwarded as a
plain option.
See
makefor constructing the client service effectfullylayerfor providing the client from already-resolved options
Signature
declare const layerConfig: (options?: { readonly apiKey?: Config.Config<Redacted.Redacted<string> | undefined> | undefined readonly apiUrl?: Config.Config<string> | undefined readonly organizationId?: Config.Config<Redacted.Redacted<string> | undefined> | undefined readonly projectId?: Config.Config<Redacted.Redacted<string> | undefined> | undefined readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined}) => Layer.Layer<OpenAiClient, Config.ConfigError, HttpClient.HttpClient>Since v4.0.0
models
Section titled “models”Service (interface)
Section titled “Service (interface)”Effect service interface for the handwritten OpenAI client.
Details
Provides the configured HTTP client plus helpers for Responses API calls, streaming Responses events, and embeddings. Transport and schema decoding failures are mapped to AiError.
Signature
export interface Service { /** * The transformed HTTP client used by this service. */ readonly client: HttpClient.HttpClient
/** * Create a response using the OpenAI responses endpoint. */ readonly createResponse: ( options: typeof OpenAiSchema.CreateResponse.Encoded ) => Effect.Effect< readonly [body: typeof OpenAiSchema.Response.Type, response: HttpClientResponse.HttpClientResponse], AiError.AiError >
/** * Create a streaming response using the OpenAI responses endpoint. */ readonly createResponseStream: ( options: Omit<typeof OpenAiSchema.CreateResponse.Encoded, "stream"> ) => Effect.Effect< readonly [ response: HttpClientResponse.HttpClientResponse, stream: Stream.Stream<typeof OpenAiSchema.ResponseStreamEvent.Type, AiError.AiError> ], AiError.AiError >
/** * Create embeddings using the OpenAI embeddings endpoint. */ readonly createEmbedding: ( options: typeof OpenAiSchema.CreateEmbeddingRequest.Encoded ) => Effect.Effect<typeof OpenAiSchema.CreateEmbeddingResponse.Type, AiError.AiError>}Since v4.0.0
options
Section titled “options”Options (type alias)
Section titled “Options (type alias)”Options for configuring the OpenAI client.
Signature
type Options = { /** * The OpenAI API key. */ readonly apiKey?: Redacted.Redacted<string> | undefined
/** * The base URL for the OpenAI API. * * @default "https://api.openai.com/v1" */ readonly apiUrl?: string | undefined
/** * Optional organization ID for multi-org accounts. */ readonly organizationId?: Redacted.Redacted<string> | undefined
/** * Optional project ID for project-scoped requests. */ readonly projectId?: Redacted.Redacted<string> | undefined
/** * Optional transformer for the HTTP client. */ readonly transformClient?: ((client: HttpClient.HttpClient) => HttpClient.HttpClient) | undefined}Since v4.0.0
services
Section titled “services”OpenAiClient (class)
Section titled “OpenAiClient (class)”Service tag for the OpenAI client.
When to use
Use when accessing or providing the OpenAI client service through Effect’s context.
See
makefor constructing an OpenAI client effectfullylayerfor providing a client from explicit optionslayerConfigfor providing a client fromConfig
Signature
declare class OpenAiClientSince v4.0.0