Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Openai Openai python Responses Create

From Leeroopedia
Knowledge Sources
Domains NLP, Text_Generation
Last Updated 2026-02-15 00:00 GMT

Overview

Concrete tool for creating model responses with streaming, structured outputs, and background execution provided by the OpenAI Python SDK.

Description

The Responses resource provides three methods: create() for standard and streaming requests, parse() for structured output parsing, and stream() for high-level streaming with event handlers. It supports text input (simple string), structured input items, tool definitions, system instructions, and multi-turn chaining via previous_response_id.

Usage

Use create() for standard responses and raw streaming. Use parse() for Pydantic-validated structured outputs. Use stream() for high-level streaming with a context manager that provides .text_stream and event iteration.

Code Reference

Source Location

  • Repository: openai-python
  • File: src/openai/resources/responses/responses.py
  • Lines: L837-920 (create impl), L978-1018 (stream impl), L1131-1171 (parse impl)

Signature

class Responses(SyncAPIResource):
    def create(
        self,
        *,
        input: Union[str, ResponseInputParam],
        model: ResponsesModel,
        stream: Optional[Literal[False]] | Literal[True] = False,
        tools: Iterable[ToolParam] | NotGiven = NOT_GIVEN,
        text: ResponseTextConfigParam | NotGiven = NOT_GIVEN,
        instructions: Optional[str] | NotGiven = NOT_GIVEN,
        temperature: Optional[float] | NotGiven = NOT_GIVEN,
        max_output_tokens: Optional[int] | NotGiven = NOT_GIVEN,
        top_p: Optional[float] | NotGiven = NOT_GIVEN,
        background: Optional[bool] | NotGiven = NOT_GIVEN,
        previous_response_id: Optional[str] | NotGiven = NOT_GIVEN,
        truncation: Optional[Literal["auto", "disabled"]] | NotGiven = NOT_GIVEN,
        user: str | NotGiven = NOT_GIVEN,
        # ... additional parameters
    ) -> Response | Stream[ResponseStreamEvent]:
        """Creates a model response."""

    def parse(
        self,
        *,
        input: Union[str, ResponseInputParam],
        model: ResponsesModel,
        text_format: type[TextFormatT],
        tools: Iterable[ParseableToolParam] | NotGiven = NOT_GIVEN,
        # ... inherits most create() parameters
    ) -> ParsedResponse[TextFormatT]:
        """Creates and parses a structured response."""

    def stream(
        self,
        *,
        input: Union[str, ResponseInputParam],
        model: ResponsesModel,
        text_format: type[TextFormatT] | NotGiven = NOT_GIVEN,
        tools: Iterable[ParseableToolParam] | NotGiven = NOT_GIVEN,
        # ... inherits most create() parameters
    ) -> ResponseStreamManager[TextFormatT]:
        """Creates a streaming response with context manager."""

Import

from openai import OpenAI
# Access via client.responses.create(), .parse(), .stream()

I/O Contract

Inputs

Name Type Required Description
input Union[str, ResponseInputParam] Yes Text prompt or structured input items
model ResponsesModel Yes Model ID (e.g., "gpt-4o")
stream bool No Enable SSE streaming (default False)
tools Iterable[ToolParam] No Tool definitions (function, web_search, file_search, etc.)
text_format type[TextFormatT] No (parse/stream) Pydantic model for structured output
instructions str No System-level instructions
temperature float No Sampling temperature
max_output_tokens int No Maximum output tokens
background bool No Run in background (retrieve later)
previous_response_id str No Chain to previous response for multi-turn

Outputs

Name Type Description
response (standard) Response Complete response with output items, usage
stream (streaming) Stream[ResponseStreamEvent] Iterator of streaming events
parsed (structured) ParsedResponse[T] Response with parsed Pydantic output
stream_manager ResponseStreamManager[T] Context manager with .text_stream

Usage Examples

Simple Text Response

from openai import OpenAI

client = OpenAI()
response = client.responses.create(
    model="gpt-4o",
    input="Explain quantum entanglement simply.",
)
print(response.output_text)

Structured Output

from pydantic import BaseModel
from openai import OpenAI

class Summary(BaseModel):
    title: str
    key_points: list[str]
    word_count: int

client = OpenAI()
response = client.responses.parse(
    model="gpt-4o",
    input="Summarize the history of the internet.",
    text_format=Summary,
)
summary = response.output_parsed
print(summary.title, summary.key_points)

Streaming

from openai import OpenAI

client = OpenAI()
with client.responses.stream(
    model="gpt-4o",
    input="Write a short poem about the sea.",
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

Multi-turn Chaining

response1 = client.responses.create(
    model="gpt-4o",
    input="What is Python?",
)

response2 = client.responses.create(
    model="gpt-4o",
    input="What are its main use cases?",
    previous_response_id=response1.id,
)
print(response2.output_text)

Background Execution

response = client.responses.create(
    model="gpt-4o",
    input="Write a detailed essay about climate change.",
    background=True,
)
# Poll for completion
import time
while response.status != "completed":
    time.sleep(2)
    response = client.responses.retrieve(response.id)
print(response.output_text)

Related Pages

Implements Principle

Requires Environment

Page Connections

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