Skip to main content
For the complete documentation index for agents and LLMs, see llms.txt.

OpenAIGenerator

Generate text using OpenAI's large language models (LLMs).

Key Features

  • Works with GPT-4, GPT-5, and o-series models through the OpenAI API.
  • Uses strings as input and output, making it ideal for use with PromptBuilder.
  • Supports streaming responses and configurable system prompts.
  • Compatible with custom OpenAI-compatible endpoints via api_base_url.
  • Configurable generation parameters via generation_kwargs.

Configuration

Authentication

You need an OpenAI API key to use this component. Connect deepset AI Platform to your OpenAI account on the Integrations page. For details, see Use OpenAI Models.

  1. Drag the OpenAIGenerator component onto the canvas from the Component Library.
  2. Click the component to open the configuration panel.
  3. On the General tab:
    1. Enter the model name, such as gpt-4o or gpt-5-mini.
  4. Go to the Advanced tab to configure the API key, API base URL, organization, timeout, max retries, generation kwargs, streaming callback, and HTTP client settings.

Connections

OpenAIGenerator receives a prompt string from PromptBuilder. It outputs replies (a list of generated strings) and meta (response metadata). Connect its replies output to AnswerBuilder for formatting answers.

Usage Example

Here's an example RAG pipeline using OpenAIGenerator:

components:
bm25_retriever:
type: haystack_integrations.components.retrievers.opensearch.bm25_retriever.OpenSearchBM25Retriever
init_parameters:
document_store:
type: haystack_integrations.document_stores.opensearch.document_store.OpenSearchDocumentStore
init_parameters:
hosts:
- ${OPENSEARCH_HOST}
index: ''
max_chunk_bytes: 104857600
embedding_dim: 768
return_embedding: false
method:
mappings:
settings:
create_index: true
http_auth:
- ${OPENSEARCH_USER}
- ${OPENSEARCH_PASSWORD}
use_ssl: true
verify_certs: false
timeout:
top_k: 10

prompt_builder:
type: haystack.components.builders.prompt_builder.PromptBuilder
init_parameters:
template: |-
You are a helpful assistant.
Answer the question based on the provided documents.
If the documents don't contain the answer, say so.

Documents:
{% for document in documents %}
{{ document.content }}
{% endfor %}

Question: {{question}}
Answer:

answer_builder:
type: haystack.components.builders.answer_builder.AnswerBuilder
init_parameters: {}
OpenAIGenerator:
type: haystack.components.generators.openai.OpenAIGenerator
init_parameters:
api_key:
type: env_var
env_vars:
- OPENAI_API_KEY
strict: false
model: gpt-5-mini
streaming_callback:
api_base_url:
organization:
system_prompt:
generation_kwargs:
timeout:
max_retries:
http_client_kwargs:

connections:
- sender: bm25_retriever.documents
receiver: prompt_builder.documents
- sender: bm25_retriever.documents
receiver: answer_builder.documents
- sender: prompt_builder.prompt
receiver: OpenAIGenerator.prompt
- sender: OpenAIGenerator.replies
receiver: answer_builder.replies

max_runs_per_component: 100

inputs:
query:
- bm25_retriever.query
- prompt_builder.question
- answer_builder.query

outputs:
answers: answer_builder.answers

metadata: {}

Parameters

Inputs

ParameterTypeDescription
promptstrThe string prompt to use for text generation.
system_promptOptional[str]The system prompt to use for text generation. If this runtime system prompt is omitted, the system prompt defined at initialization time is used.
streaming_callbackOptional[StreamingCallbackT]A callback function called when a new token is received from the stream.
generation_kwargsOptional[Dict[str, Any]]Additional keyword arguments for text generation. These parameters override the parameters passed in the __init__ method. For more details, see OpenAI documentation.

Outputs

ParameterTypeDescription
repliesList[str]A list of strings containing the generated responses.
metaList[Dict[str, Any]]A list of dictionaries containing metadata for each response, including model info and usage.

Init Parameters

These are the parameters you can configure in Pipeline Builder:

ParameterTypeDefaultDescription
api_keySecretSecret.from_env_var('OPENAI_API_KEY')The OpenAI API key to connect to OpenAI.
modelstrgpt-5-miniThe name of the model to use.
streaming_callbackOptional[StreamingCallbackT]NoneA callback function called when a new token is received from the stream. The callback function accepts StreamingChunk as an argument.
api_base_urlOptional[str]NoneAn optional base URL.
organizationOptional[str]NoneThe Organization ID. For help, see Setting up your organization.
system_promptOptional[str]NoneThe system prompt to use for text generation. If not provided, the system prompt is omitted, and the default system prompt of the model is used.
generation_kwargsOptional[Dict[str, Any]]NoneOther parameters to use for the model, sent directly to the OpenAI endpoint. See OpenAI documentation for more details. Supported parameters include: max_tokens, temperature, top_p, n, stop, presence_penalty, frequency_penalty, logit_bias.
timeoutOptional[float]30.0Timeout for OpenAI client calls. If not set, it is inferred from the OPENAI_TIMEOUT environment variable or set to 30.
max_retriesOptional[int]fiveMaximum retries to establish contact with OpenAI if it returns an internal error. If not set, it is inferred from the OPENAI_MAX_RETRIES environment variable or set to five.
http_client_kwargsOptional[Dict[str, Any]]NoneA dictionary of keyword arguments to configure a custom httpx.Client or httpx.AsyncClient. For more information, see the HTTPX documentation.

Run Method Parameters

These are the parameters you can configure for the component's run() method. This means you can pass these parameters at query time through the API, in Playground, or when running a job. For details, see Modify Pipeline Parameters at Query Time.

ParameterTypeDefaultDescription
promptstrThe string prompt to use for text generation.
system_promptOptional[str]NoneThe system prompt to use for text generation. If this runtime system prompt is omitted, the system prompt defined at initialization time is used.
streaming_callbackOptional[StreamingCallbackT]NoneA callback function called when a new token is received from the stream.
generation_kwargsOptional[Dict[str, Any]]NoneAdditional keyword arguments for text generation. These parameters override the parameters passed in the __init__ method. For more details, see OpenAI documentation.