responses

package
v1.0.0-beta.98 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 3, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package responses is the self-hosted JSON wire-format layer for OpenAI's Responses API (POST /v1/responses). It is a sibling of model/wire (which speaks ChatCompletion) and the second top-level wire shape carried in the wire-package family.

The Responses API is structurally distinct from ChatCompletions — it uses a typed-array input/output model (InputItem / OutputItem) rather than the message/choices model, dispatches polymorphism through a "type" discriminator on each item, and surfaces reasoning as a first-class item kind rather than an out-of-band field. The ChatCompletion wire types do not apply; this package owns its own Request, Response, item variants, and SSE streaming protocol.

Phase 1 (this package's initial cut) implements the non-streaming request/response path covering function-calling and reasoning echo in stateless mode (store=false). Hosted tools (file_search, web_search_preview, code_interpreter, computer_use_preview, image_generation) are out of scope and not modeled here; the surface stays narrow until a concrete consumer asks. Streaming (typed-event SSE) lands in Phase 2 as a sibling file.

Reasoning-record echo (the "ReasoningRecord opaque blob round-trip" per ADR-051 D3) is handled at the higher agentic-model adapter layer; this package merely models the wire shape of reasoning items and exposes their fields. See ADR-051 for the architectural motivation and ADR-037 for the parent wire-package shape.

Index

Constants

View Source
const (
	// Lifecycle events: payload is {response: Response}.
	EventTypeResponseCreated    = "response.created"
	EventTypeResponseInProgress = "response.in_progress"
	EventTypeResponseCompleted  = "response.completed"
	EventTypeResponseFailed     = "response.failed"
	EventTypeResponseCancelled  = "response.cancelled"
	EventTypeResponseIncomplete = "response.incomplete"

	// Output item events: payload carries an OutputItem at OutputIndex.
	EventTypeOutputItemAdded = "response.output_item.added"
	EventTypeOutputItemDone  = "response.output_item.done"

	// Content part events: payload carries a ContentPart at
	// (OutputIndex, ContentIndex) inside a message item.
	EventTypeContentPartAdded = "response.content_part.added"
	EventTypeContentPartDone  = "response.content_part.done"

	// Streaming text events: delta accumulates, done snapshots final.
	EventTypeOutputTextDelta = "response.output_text.delta"
	EventTypeOutputTextDone  = "response.output_text.done"

	// Refusal events: same shape as text events but for refusals.
	EventTypeRefusalDelta = "response.refusal.delta"
	EventTypeRefusalDone  = "response.refusal.done"

	// Function-call arguments events: delta accumulates JSON-encoded
	// arguments string, done snapshots final.
	EventTypeFunctionCallArgumentsDelta = "response.function_call_arguments.delta"
	EventTypeFunctionCallArgumentsDone  = "response.function_call_arguments.done"

	// Reasoning summary part events: payload carries a SummaryPart at
	// (OutputIndex, SummaryIndex). Note Part is a RawMessage —
	// caller decodes via SummaryPart() helper since the "part" field
	// shape differs from content_part.* events.
	EventTypeReasoningSummaryPartAdded = "response.reasoning_summary_part.added"
	EventTypeReasoningSummaryPartDone  = "response.reasoning_summary_part.done"

	// Reasoning summary text events: delta accumulates, done
	// snapshots final.
	EventTypeReasoningSummaryTextDelta = "response.reasoning_summary_text.delta"
	EventTypeReasoningSummaryTextDone  = "response.reasoning_summary_text.done"

	// Error event: payload is {error: APIError}.
	EventTypeError = "error"
)

Event type constants enumerate the Responses SSE event types this package recognizes. The closed set covers function-calling + reasoning echo (Phase 1+2 scope); hosted-tool events (code interpreter, computer use, file search, etc.) are not modeled. Unknown event types decode into Event with Type populated and fields zero — callers can ignore them or surface them via Type.

View Source
const (
	// Input/output item types.
	ItemTypeMessage            = "message"
	ItemTypeFunctionCall       = "function_call"
	ItemTypeFunctionCallOutput = "function_call_output"
	ItemTypeReasoning          = "reasoning"

	// Content-part types.
	ContentTypeInputText  = "input_text"
	ContentTypeOutputText = "output_text"
	ContentTypeRefusal    = "refusal"

	// Summary-part types.
	SummaryTypeText = "summary_text"

	// Role values on message items.
	RoleUser      = "user"
	RoleSystem    = "system"
	RoleDeveloper = "developer"
	RoleAssistant = "assistant"
)

Variant-type constants. The Responses API discriminates polymorphic items via a "type" string field; these constants name the values this package recognizes in Phase 1.

View Source
const DefaultMaxFrameSize = 16 * 1024 * 1024

DefaultMaxFrameSize is the default per-SSE-frame buffer ceiling. Matches the model/wire ChatCompletion stream cap (16 MiB) — Responses output_text deltas are typically small but reasoning summary and function_call_arguments blobs can grow.

Variables

View Source
var DecodeError = wire.DecodeError

DecodeError parses a Responses error body into an APIError. Re-exported from model/wire; the body shape matches.

Functions

This section is empty.

Types

type APIError

type APIError = wire.APIError

APIError is the decoded non-2xx error from a Responses call. Re-exported from model/wire — the envelope shape is identical to ChatCompletion's per ADR-051 ("Same envelope shape; same HTTP status semantics" in the structural-delta table). Callers can errors.As on either *wire.APIError or *responses.APIError; they are the same type.

type Accumulator

type Accumulator struct {
	// contains filtered or unexported fields
}

Accumulator builds a Response from a Responses event stream.

Per the API contract, response.completed (and the other terminal lifecycle events) carry the full final Response object. The accumulator promotes that to the terminal state when seen and Final returns it directly. As a fallback for streams that terminate without a lifecycle event (server-side cut, body truncation, etc.), the accumulator builds an incremental Response from the per-item / per-delta events so partial-stream callers still get something usable.

Accumulator is NOT safe for concurrent use; one goroutine drives one stream.

func NewAccumulator

func NewAccumulator() *Accumulator

NewAccumulator constructs a fresh Accumulator.

func (*Accumulator) Add

func (a *Accumulator) Add(ev *Event) error

Add merges one Event into the accumulator. Errors are returned for malformed events (e.g. delta with no output_index) so callers can decide whether to halt the stream or skip; the recommended behavior for the agentic-model adapter layer is to log + skip since the Responses API itself promises well-formed events under normal conditions.

func (*Accumulator) Final

func (a *Accumulator) Final() *Response

Final returns the accumulated Response. Prefers the terminal lifecycle snapshot when present; falls back to the incrementally- assembled Response built from item/delta events. Returns nil if no relevant events were seen.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is the OpenAI Responses API client. Safe for concurrent use; callers typically construct one Client per endpoint and reuse it across requests.

func NewClient

func NewClient(cfg ClientConfig) (*Client, error)

NewClient validates cfg and returns a Client. Returns an error if HTTPClient or BaseURL is missing.

func (*Client) Responses

func (c *Client) Responses(ctx context.Context, req *Request) (*Response, error)

Responses executes a non-streaming Responses call. Returns a decoded response on HTTP 2xx, or *APIError with the status code recorded on any other status. Network and decode errors return as regular errors.

Streaming callers should use ResponsesStream instead; passing req.Stream=true here returns an error to make the misuse loud (Responses always forces Stream=false on the wire).

func (*Client) ResponsesStream

func (c *Client) ResponsesStream(ctx context.Context, req *Request) (*Stream, error)

ResponsesStream executes a streaming Responses call. Returns a *Stream the caller drives via Recv until io.EOF; callers MUST call Close when done. On non-2xx the response body is read fully and returned as *APIError; the *Stream is nil in that case.

The request's Stream flag is forced to true before send — the caller does not need to set it.

type ClientConfig

type ClientConfig struct {
	// BaseURL is the OpenAI-compat root, e.g. "https://api.openai.com/v1".
	// If ResponsesURL is set, it takes precedence.
	BaseURL string

	// HTTPClient is the transport. Callers should use
	// model.NewHTTPClient to construct one with the framework's
	// connection-hygiene defaults.
	HTTPClient *http.Client

	// AuthHeader, if non-empty, is set as the Authorization header
	// on every request. Typically "Bearer <key>".
	AuthHeader string

	// ExtraHeaders are added to every request before send. Useful for
	// provider-specific headers.
	ExtraHeaders http.Header

	// ResponsesURL overrides BaseURL+/responses. Empty means derive
	// from BaseURL.
	ResponsesURL string

	// MaxFrameSize caps the per-SSE-frame buffer in streaming mode.
	// Phase 1 client is non-streaming; the field is carried for the
	// Phase 2 streaming addition.
	MaxFrameSize int
}

ClientConfig configures a Responses Client. Mirror of wire.ClientConfig; the two clients share auth/transport plumbing but speak different wire shapes. BaseURL and HTTPClient are required; everything else is optional.

type ContentPart

type ContentPart struct {
	// Type is one of "input_text", "output_text", "refusal".
	Type string `json:"type"`

	// Text carries the body for text-class parts.
	Text string `json:"text,omitempty"`

	// Refusal carries the refusal body when Type == "refusal".
	Refusal string `json:"refusal,omitempty"`

	// Annotations are inline citations/URL refs the model may attach
	// to output_text. Carried as RawMessage; this package does not
	// model the shape, which has evolved across SDK versions.
	Annotations json.RawMessage `json:"annotations,omitempty"`
}

ContentPart is a typed content element inside a message item. Variants:

  • input_text: user-supplied text on InputItem messages
  • output_text: assistant text on OutputItem messages
  • refusal: assistant refusal text

Input image/file/audio parts are out of scope for Phase 1.

type Event

type Event struct {
	// Type is the SSE event name (e.g. "response.output_text.delta").
	// Mirrors the `event:` SSE line and the data payload's `type`
	// field, which the API contract holds equal.
	Type string `json:"type"`

	// SequenceNumber is the monotonically increasing event sequence
	// the API ships on every event. Useful for de-duplication on
	// reconnect; we don't reconnect (stateless one-shot stream) but
	// the field is carried for trace correlation.
	SequenceNumber int `json:"sequence_number,omitempty"`

	// Response carries the full Response on lifecycle events
	// (response.created, response.completed, response.failed, etc.).
	Response *Response `json:"response,omitempty"`

	// OutputIndex addresses the OutputItem inside Response.Output
	// that this event targets. Populated on item/content/text/refusal/
	// arguments/summary events.
	OutputIndex *int `json:"output_index,omitempty"`

	// ContentIndex addresses the ContentPart inside an OutputItem's
	// Content array. Populated on content_part.*, output_text.*,
	// refusal.* events.
	ContentIndex *int `json:"content_index,omitempty"`

	// SummaryIndex addresses the SummaryPart inside a reasoning
	// OutputItem's Summary array. Populated on reasoning_summary_*
	// events.
	SummaryIndex *int `json:"summary_index,omitempty"`

	// ItemID echoes the OutputItem.ID this event targets. Populated
	// on content/text/refusal/arguments/summary events.
	ItemID string `json:"item_id,omitempty"`

	// Item carries the OutputItem on output_item.added /
	// output_item.done.
	Item *OutputItem `json:"item,omitempty"`

	// Part carries the polymorphic part payload on content_part.* and
	// reasoning_summary_part.* events. Decode via ContentPart() or
	// SummaryPart() helpers based on Type.
	Part json.RawMessage `json:"part,omitempty"`

	// Delta accumulates partial text on output_text.delta,
	// refusal.delta, function_call_arguments.delta, and
	// reasoning_summary_text.delta events.
	Delta string `json:"delta,omitempty"`

	// Text snapshots final text on output_text.done and
	// reasoning_summary_text.done events.
	Text string `json:"text,omitempty"`

	// Arguments snapshots final JSON-encoded arguments on
	// function_call_arguments.done.
	Arguments string `json:"arguments,omitempty"`

	// Refusal snapshots final refusal body on refusal.done.
	Refusal string `json:"refusal,omitempty"`

	// Error carries the APIError on error events.
	Error json.RawMessage `json:"error,omitempty"`
}

Event is a typed Responses SSE event. The Type field is the discriminator; other fields are populated based on the variant. Use the helper methods (ContentPart, SummaryPart, APIError) to decode polymorphic fields by event type.

Producer convention: the typed payload structs in OpenAI's documented shapes are flattened into this union for simplicity. Unknown event types still decode (Type populated, payload fields zero or RawMessage); callers iterate forward without error.

func (*Event) APIError

func (e *Event) APIError() (*APIError, error)

APIError decodes Error as an APIError. Only valid on error events; returns an error if Type is incompatible or Error is empty.

func (*Event) ContentPart

func (e *Event) ContentPart() (*ContentPart, error)

ContentPart decodes Part as a ContentPart. Only valid on content_part.* events; returns an error if Type is incompatible or Part is empty.

func (*Event) SummaryPart

func (e *Event) SummaryPart() (*SummaryPart, error)

SummaryPart decodes Part as a SummaryPart. Only valid on reasoning_summary_part.* events; returns an error if Type is incompatible or Part is empty.

type InputItem

type InputItem struct {
	// Type is the variant discriminator. Required.
	Type string `json:"type"`

	// ID echoes the provider-assigned identifier for items echoed
	// from a prior response (reasoning, function_call). Optional on
	// items the caller constructs locally.
	ID string `json:"id,omitempty"`

	// Role is set when Type == "message". Values: "user", "system",
	// "developer", "assistant". The translator should emit "developer"
	// for system-prompt-class messages on Responses (system → developer)
	// per ADR-051 open question 2.
	Role string `json:"role,omitempty"`

	// Content is the typed content-parts array on message items.
	Content []ContentPart `json:"content,omitempty"`

	// CallID is the function-call correlation token shared between
	// function_call (echo) and function_call_output items. Set on
	// both types.
	CallID string `json:"call_id,omitempty"`

	// Name is the function name on function_call items.
	Name string `json:"name,omitempty"`

	// Arguments is the JSON-encoded arguments string on function_call
	// items. Wire-shape mirrors ChatCompletion's
	// tool_calls[].function.arguments — opaque string holding a JSON
	// object.
	Arguments string `json:"arguments,omitempty"`

	// Output is the tool-result body on function_call_output items.
	// Opaque string — typically a JSON-encoded object, but the API
	// accepts any string the caller wants the model to consume.
	Output string `json:"output,omitempty"`

	// Summary is the reasoning summary on reasoning items. Carried
	// as typed parts (mirror of the response shape).
	Summary []SummaryPart `json:"summary,omitempty"`

	// EncryptedContent is the opaque blob echoed on reasoning items
	// in stateless mode. Treated as bytes; do not parse.
	EncryptedContent string `json:"encrypted_content,omitempty"`

	// Status is optionally present on items echoed from a prior
	// response. Possible values include "completed", "in_progress",
	// "incomplete". Producers leave empty on items they construct.
	Status string `json:"status,omitempty"`
}

InputItem is the discriminated-union element of Request.Input. The Type field selects the populated subset of fields. The closed set for Phase 1: "message", "function_call", "function_call_output", "reasoning". Unknown types decode without error (forward-compat) and re-emit verbatim through their tagged fields.

Producer convention: callers construct via the New* helpers (NewInputMessage, NewInputFunctionCall, etc.) to avoid setting incoherent field combinations.

func NewInputDeveloperMessage

func NewInputDeveloperMessage(text string) InputItem

NewInputDeveloperMessage constructs a developer-role message InputItem. Use this for system-prompt-class instructions per ADR-051 open question 2 (system → developer translation on Responses); pass system-prompt prose verbatim and let the adapter pick this constructor.

func NewInputFunctionCall

func NewInputFunctionCall(callID, name, arguments string) InputItem

NewInputFunctionCall constructs a function_call InputItem echoing a prior assistant tool call. callID must match the call_id the model emitted; arguments is the JSON-encoded arguments string.

func NewInputFunctionCallOutput

func NewInputFunctionCallOutput(callID, output string) InputItem

NewInputFunctionCallOutput constructs a function_call_output InputItem carrying a tool result. callID correlates to the prior function_call; output is the result body (typically JSON-encoded).

func NewInputReasoning

func NewInputReasoning(id, encryptedContent string, summary []SummaryPart) InputItem

NewInputReasoning constructs a reasoning InputItem echoing a prior response's reasoning blob. id and encryptedContent must come verbatim from the response item. The summary slice is optional — pass nil to omit.

func NewInputUserMessage

func NewInputUserMessage(text string) InputItem

NewInputUserMessage constructs a user-role message InputItem with a single input_text content part. Convenience for the common case; callers wanting multi-part content build the InputItem directly.

func (*InputItem) IsFunctionCall

func (i *InputItem) IsFunctionCall() bool

IsFunctionCall reports whether the item is a function-call echo.

func (*InputItem) IsFunctionCallOutput

func (i *InputItem) IsFunctionCallOutput() bool

IsFunctionCallOutput reports whether the item is a tool result.

func (*InputItem) IsMessage

func (i *InputItem) IsMessage() bool

IsMessage reports whether the item is a message variant.

func (*InputItem) IsReasoning

func (i *InputItem) IsReasoning() bool

IsReasoning reports whether the item is a reasoning echo.

func (InputItem) MarshalJSON

func (i InputItem) MarshalJSON() ([]byte, error)

MarshalJSON ensures the `summary` field is always present on reasoning input items even when the local slice is nil or empty. The OpenAI Responses API rejects echoed reasoning items without a summary field with HTTP 400 missing_required_parameter, even though `[]` is acceptable. Caught by the ADR-051 PR 4 live reasoning-echo test before tag.

Non-reasoning items marshal through the default path (omitempty honored on Summary).

type InputTokensDetails

type InputTokensDetails struct {
	CachedTokens int `json:"cached_tokens,omitempty"`
}

InputTokensDetails breaks down prompt-side token attribution.

type OutputItem

type OutputItem struct {
	// Type is the variant discriminator. Required.
	Type string `json:"type"`

	// ID is the provider-assigned identifier. Required on all output
	// items.
	ID string `json:"id"`

	// Status reports the item's lifecycle state. Common values:
	// "completed", "in_progress", "incomplete".
	Status string `json:"status,omitempty"`

	// Role applies to message items. "assistant" in practice.
	Role string `json:"role,omitempty"`

	// Content is the typed content-parts array on message items.
	Content []ContentPart `json:"content,omitempty"`

	// CallID is the function-call correlation token. Set on
	// function_call items.
	CallID string `json:"call_id,omitempty"`

	// Name is the function name on function_call items.
	Name string `json:"name,omitempty"`

	// Arguments is the JSON-encoded arguments string on function_call
	// items.
	Arguments string `json:"arguments,omitempty"`

	// Summary is the reasoning summary on reasoning items.
	Summary []SummaryPart `json:"summary,omitempty"`

	// EncryptedContent is the opaque reasoning blob the API emits in
	// stateless mode. The caller must echo it back verbatim on the
	// next turn's InputItem to maintain reasoning continuity.
	EncryptedContent string `json:"encrypted_content,omitempty"`
}

OutputItem is the discriminated-union element of Response.Output. Variant set parallels InputItem with output-side extensions: "message" assistant replies, "function_call" tool calls, "reasoning" emitted blobs.

func (*OutputItem) IsFunctionCall

func (o *OutputItem) IsFunctionCall() bool

IsFunctionCall reports whether the output item is a function call.

func (*OutputItem) IsMessage

func (o *OutputItem) IsMessage() bool

IsMessage reports whether the output item is a message.

func (*OutputItem) IsReasoning

func (o *OutputItem) IsReasoning() bool

IsReasoning reports whether the output item is a reasoning blob.

func (*OutputItem) OutputText

func (o *OutputItem) OutputText() string

OutputText returns the concatenation of output_text content parts on a message OutputItem. Empty for non-message items or items containing only refusals. Convenience helper for the common "assistant said X" extraction; callers needing structured annotations or refusal handling walk Content directly.

func (*OutputItem) RefusalText

func (o *OutputItem) RefusalText() string

RefusalText returns the first refusal content part's body on a message OutputItem, or "" if no refusal is present.

type OutputTokensDetails

type OutputTokensDetails struct {
	ReasoningTokens int `json:"reasoning_tokens,omitempty"`
}

OutputTokensDetails breaks down completion-side token attribution. ReasoningTokens accounts the o-series/GPT-5.5 internal reasoning budget separately from emitted output tokens.

type ReasoningParams

type ReasoningParams struct {
	// Effort is "minimal", "low", "medium", or "high". Empty falls
	// through to the model default.
	Effort string `json:"effort,omitempty"`

	// Summary controls reasoning summary emission. "auto", "concise",
	// "detailed", or empty for none.
	Summary string `json:"summary,omitempty"`
}

ReasoningParams configures reasoning behavior on o-series and GPT-5.5-class models. Carried on both Request (input) and Response (echo); the same shape applies in both directions.

type Request

type Request struct {
	// Model is the OpenAI model identifier, e.g. "gpt-5.5", "o4-mini",
	// "codex-mini-latest".
	Model string `json:"model"`

	// Input is the heterogeneous input items array. Each element is a
	// typed item (message, function_call echo, function_call_output,
	// reasoning echo). See InputItem.
	Input []InputItem `json:"input"`

	// Instructions optionally pins a system-prompt-class instruction.
	// Equivalent in role to ChatCompletion's "system" message but
	// passed as a top-level field. Recommended for stable persona
	// prose that doesn't belong inline with conversational input.
	Instructions string `json:"instructions,omitempty"`

	// Tools enumerates the function tools available to the model.
	// Hosted tools (file_search, web_search_preview, etc.) are out of
	// scope for Phase 1; future addition extends the Tool type with a
	// discriminator.
	Tools []Tool `json:"tools,omitempty"`

	// ToolChoice controls tool-selection behavior. Accepts the same
	// string values as ChatCompletion ("auto", "required", "none")
	// or an object {"type":"function","name":"..."} forcing a
	// specific tool. json.RawMessage carries the variant.
	ToolChoice json.RawMessage `json:"tool_choice,omitempty"`

	// Reasoning controls reasoning-effort and summary-emission for
	// the o-series and GPT-5.5-class models. The Responses endpoint
	// is the only endpoint where reasoning_effort combines with
	// tool_choice on these model classes.
	Reasoning *ReasoningParams `json:"reasoning,omitempty"`

	// Text controls the output text format. The Responses API uses
	// text.format here rather than the ChatCompletion top-level
	// response_format. The two shapes are not 1:1; the adapter layer
	// translates.
	Text *TextParams `json:"text,omitempty"`

	// Temperature, TopP, MaxOutputTokens carry the standard sampling
	// controls. Names match the Responses API ("max_output_tokens",
	// not "max_tokens" — the two endpoints differ).
	Temperature     *float64 `json:"temperature,omitempty"`
	TopP            *float64 `json:"top_p,omitempty"`
	MaxOutputTokens int      `json:"max_output_tokens,omitempty"`

	// Store enables OpenAI-side conversation state persistence.
	// Stateless mode (store=false) is what this client uses per
	// ADR-051 D2 — caller owns full history and echoes reasoning
	// items per turn. The field is a pointer so the zero value is
	// distinguishable from explicit-false (the API default is true).
	Store *bool `json:"store,omitempty"`

	// PreviousResponseID threads server-side state when Store=true.
	// Unused in stateless mode; carried here for completeness.
	PreviousResponseID string `json:"previous_response_id,omitempty"`

	// Stream selects SSE streaming mode. Phase 1 client only supports
	// false; streaming lands in Phase 2.
	Stream bool `json:"stream,omitempty"`

	// User is the optional end-user identifier for abuse monitoring,
	// per OpenAI's documented usage.
	User string `json:"user,omitempty"`

	// Metadata is the optional operator-provided key/value pairs the
	// API echoes back on the response. Capped at 16 keys, 64-char
	// keys, 512-char values per OpenAI's docs.
	Metadata map[string]string `json:"metadata,omitempty"`

	// Include opts into response fields the API otherwise omits.
	// Most consequential for our path: "reasoning.encrypted_content"
	// — without this opt-in, the API does NOT emit encrypted_content
	// on reasoning output items in stateless mode, breaking
	// cross-turn echo silently. The translator sets this
	// automatically when reasoning is configured (ADR-051 D2).
	//
	// Other documented values (out of scope for Phase 1):
	// "message.input_image.image_url", "file_search_call.results",
	// "code_interpreter_call.outputs".
	Include []string `json:"include,omitempty"`
}

Request is the POST /v1/responses request body. Phase 1 scope: non-streaming, stateless (store=false), function-calling and reasoning. Hosted tools and image inputs are out of scope.

The Input field holds the heterogeneous typed array that replaces ChatCompletion's messages array. See InputItem for the variant set.

type Response

type Response struct {
	// ID is the provider-assigned response identifier ("resp_..."),
	// usable for previous_response_id threading when Store=true (not
	// used in stateless mode but echoed for trace correlation).
	ID string `json:"id"`

	// Object is the response object type, always "response" on
	// success. Carried for parity with OpenAI's documented shape.
	Object string `json:"object,omitempty"`

	// CreatedAt is the response creation Unix timestamp.
	CreatedAt int64 `json:"created_at,omitempty"`

	// Status is "completed", "failed", "in_progress", "cancelled",
	// or "incomplete".
	Status string `json:"status,omitempty"`

	// Error is populated when Status == "failed". The shape mirrors
	// APIError but is embedded in the response body rather than
	// raised as an HTTP error.
	Error json.RawMessage `json:"error,omitempty"`

	// IncompleteDetails is populated when Status == "incomplete".
	IncompleteDetails json.RawMessage `json:"incomplete_details,omitempty"`

	// Instructions echoes back the request's Instructions field.
	Instructions string `json:"instructions,omitempty"`

	// MaxOutputTokens echoes back the request's MaxOutputTokens.
	MaxOutputTokens int `json:"max_output_tokens,omitempty"`

	// Model is the served model identifier (may differ from requested
	// when the provider routes through a model alias).
	Model string `json:"model"`

	// Output is the heterogeneous emitted items array.
	Output []OutputItem `json:"output"`

	// PreviousResponseID is set when this response continued from a
	// prior response. Empty in stateless mode.
	PreviousResponseID string `json:"previous_response_id,omitempty"`

	// Reasoning echoes back the request's Reasoning settings.
	Reasoning *ReasoningParams `json:"reasoning,omitempty"`

	// Store reports whether server-side persistence is active.
	Store *bool `json:"store,omitempty"`

	// Temperature, TopP echo back the sampling controls.
	Temperature *float64 `json:"temperature,omitempty"`
	TopP        *float64 `json:"top_p,omitempty"`

	// Text echoes back the request's Text settings.
	Text *TextParams `json:"text,omitempty"`

	// ToolChoice and Tools echo back the request configuration.
	ToolChoice json.RawMessage `json:"tool_choice,omitempty"`
	Tools      []Tool          `json:"tools,omitempty"`

	// Truncation reports the truncation policy applied. Values
	// include "auto", "disabled". Carried as string for forward
	// compat.
	Truncation string `json:"truncation,omitempty"`

	// Usage reports token consumption.
	Usage *Usage `json:"usage,omitempty"`

	// User echoes back the request's User identifier.
	User string `json:"user,omitempty"`

	// Metadata echoes back the request's Metadata map.
	Metadata map[string]string `json:"metadata,omitempty"`
}

Response is the POST /v1/responses response body. Status reports the run's terminal state ("completed", "failed", "in_progress", "cancelled", "incomplete"); the Output array is the heterogeneous emitted items.

type Stream

type Stream struct {
	// contains filtered or unexported fields
}

Stream consumes a Responses Server-Sent-Events response body, decoding each frame into an Event. The caller iterates by calling Recv until io.EOF.

Stream is NOT safe for concurrent use; one goroutine drives a stream from start to finish, then calls Close.

func (*Stream) Close

func (s *Stream) Close() error

Close releases the underlying response body. Safe to call multiple times.

func (*Stream) Recv

func (s *Stream) Recv() (*Event, error)

Recv returns the next decoded event. Returns io.EOF when the underlying body has ended or a terminal lifecycle event (response.completed / failed / cancelled / incomplete) has been emitted and consumed. Subsequent calls after EOF continue to return io.EOF.

Note: the Responses API does NOT emit a [DONE] sentinel — terminal lifecycle events ARE the end signal. The stream body closes shortly after; the underlying io.EOF lands on the next Recv. Recv does not auto-terminate on lifecycle events so the caller sees the terminal Event.

type SummaryPart

type SummaryPart struct {
	Type string `json:"type"`
	Text string `json:"text,omitempty"`
}

SummaryPart is one segment of a reasoning item's Summary array. Today the only documented variant is type:"summary_text"; the field set is open for future additions.

type TextParams

type TextParams struct {
	// Format is the JSON-schema or json_object envelope. Carried as
	// RawMessage so callers can pass through the shape OpenAI
	// documents without this package taking a hard dependency on
	// the schema layout (which has evolved across SDK versions).
	Format json.RawMessage `json:"format,omitempty"`
}

TextParams controls the output text format on Responses requests. Distinct from ChatCompletion's top-level ResponseFormat — the Responses API nests format under text. Carried on both Request (input) and Response (echo).

type Tool

type Tool struct {
	Type        string          `json:"type"`
	Name        string          `json:"name"`
	Description string          `json:"description,omitempty"`
	Parameters  json.RawMessage `json:"parameters,omitempty"`
	Strict      bool            `json:"strict,omitempty"`
}

Tool is a function tool declaration. Responses-shape uses a flat {type, name, description, parameters, strict} layout — distinct from ChatCompletion's nested {type:"function", function:{...}}. Hosted tools (file_search, web_search_preview, code_interpreter, computer_use_preview, image_generation) are not modeled for Phase 1. Carried on both Request (input) and Response (echo).

type Usage

type Usage struct {
	InputTokens        int                 `json:"input_tokens"`
	InputTokensDetails *InputTokensDetails `json:"input_tokens_details,omitempty"`

	OutputTokens        int                  `json:"output_tokens"`
	OutputTokensDetails *OutputTokensDetails `json:"output_tokens_details,omitempty"`

	TotalTokens int `json:"total_tokens"`
}

Usage reports token consumption on a Response. Field names match the Responses API ("input_tokens", "output_tokens") — distinct from ChatCompletion's "prompt_tokens"/"completion_tokens".

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL