OpenAI Agents History
Convert conversation history from OpenAI Agents SDK to DeltaKit messages.
If your backend uses the OpenAI Agents SDK, you can convert agent run history into DeltaKit messages using fromOpenAiAgents.
The Problem
The OpenAI Agents SDK stores conversation history as TResponseInputItem, a list of 28+ item types including messages, tool calls, tool outputs, reasoning, and internal control items. This format is not directly usable in a chat UI.
fromOpenAiAgents converts these items into a clean Message<ContentPart>[] array.
Backend Setup
Expose an endpoint that returns the agent's history:
from agents import Agent, Runner
agent = Agent(name="MyAgent", instructions="You are helpful.")
@app.get("/api/chat/history/{session_id}")
async def get_history(session_id: str):
# Retrieve stored history for this session
result = await Runner.run(agent, input=[], session_id=session_id)
return result.to_input_list()Usage
import { fromOpenAiAgents } from "@deltakit/core";
// Load history from your API
const history = await fetch("/api/chat/history/session_123").then((r) => r.json());
// Convert to DeltaKit messages
const messages = fromOpenAiAgents(history);
// => Message<ContentPart>[]For using converted messages with the React hook, see Initial Messages.
What Gets Converted
Given an agent history like:
[
{ "role": "user", "content": "What's the weather?" },
{ "type": "function_call", "name": "get_weather", "arguments": "{\"city\":\"London\"}", "call_id": "call_1" },
{ "type": "function_call_output", "call_id": "call_1", "output": "Sunny, 18°C" },
{ "type": "message", "role": "assistant", "content": [{ "type": "output_text", "text": "It's sunny and 18°C in London!" }] }
]fromOpenAiAgents produces:
[
{
id: "openai_0",
role: "user",
parts: [{ type: "text", text: "What's the weather?" }],
},
{
id: "openai_1",
role: "assistant",
parts: [
{
type: "tool_call",
tool_name: "get_weather",
argument: '{"city":"London"}',
callId: "call_1",
result: "Sunny, 18°C",
},
{ type: "text", text: "It's sunny and 18°C in London!" },
],
},
]Notice how:
- The user message becomes a simple
Messagewith aTextPart. - The function call, its output, and the assistant text are grouped into a single assistant
Message. - The tool result is attached to its
ToolCallPartviacallId. - System/developer messages and internal items are filtered out.