Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Openai Openai python Compacted Response

From Leeroopedia
Knowledge Sources
Domains API_Types, Python
Last Updated 2026-02-15 00:00 GMT

Overview

Concrete type for representing a compacted response provided by the openai-python SDK.

Description

CompactedResponse is a Pydantic model class that represents a compacted conversation response from the OpenAI API. It extends BaseModel and includes a unique identifier, a Unix timestamp for creation, an object type field fixed to "response.compaction", a list of ResponseOutputItem objects representing the compacted output (all user messages followed by a single compaction item), and a ResponseUsage object providing token accounting for the compaction pass including cached, reasoning, and total tokens.

Usage

Import this type when working with conversation compaction in the Responses API. The compacted response is returned when a long conversation is condensed to reduce token usage while preserving context.

Code Reference

Source Location

Signature

class CompactedResponse(BaseModel):
    id: str
    created_at: int
    object: Literal["response.compaction"]
    output: List[ResponseOutputItem]
    usage: ResponseUsage

Import

from openai.types.responses import CompactedResponse

I/O Contract

Fields

Name Type Required Description
id str Yes The unique identifier for the compacted response.
created_at int Yes Unix timestamp (in seconds) when the compacted conversation was created.
object Literal["response.compaction"] Yes The object type. Always "response.compaction".
output List[ResponseOutputItem] Yes The compacted list of output items (user messages followed by a compaction item).
usage ResponseUsage Yes Token accounting for the compaction pass, including cached, reasoning, and total tokens.

Usage Examples

from openai.types.responses import CompactedResponse

# Access fields from a compacted response
compacted: CompactedResponse = ...
print(f"Compaction ID: {compacted.id}")
print(f"Created at: {compacted.created_at}")
print(f"Output items: {len(compacted.output)}")
print(f"Total tokens: {compacted.usage.total_tokens}")

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment