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
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.
- Drag the
OpenAIGeneratorcomponent onto the canvas from the Component Library. - Click the component to open the configuration panel.
- On the General tab:
- Enter the model name, such as
gpt-4oorgpt-5-mini.
- Enter the model name, such as
- 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
| Parameter | Type | Description |
|---|---|---|
prompt | str | The string prompt to use for text generation. |
system_prompt | Optional[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_callback | Optional[StreamingCallbackT] | A callback function called when a new token is received from the stream. |
generation_kwargs | Optional[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
| Parameter | Type | Description |
|---|---|---|
replies | List[str] | A list of strings containing the generated responses. |
meta | List[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:
| Parameter | Type | Default | Description |
|---|---|---|---|
api_key | Secret | Secret.from_env_var('OPENAI_API_KEY') | The OpenAI API key to connect to OpenAI. |
model | str | gpt-5-mini | The name of the model to use. |
streaming_callback | Optional[StreamingCallbackT] | None | A callback function called when a new token is received from the stream. The callback function accepts StreamingChunk as an argument. |
api_base_url | Optional[str] | None | An optional base URL. |
organization | Optional[str] | None | The Organization ID. For help, see Setting up your organization. |
system_prompt | Optional[str] | None | The 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_kwargs | Optional[Dict[str, Any]] | None | Other 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. |
timeout | Optional[float] | 30.0 | Timeout for OpenAI client calls. If not set, it is inferred from the OPENAI_TIMEOUT environment variable or set to 30. |
max_retries | Optional[int] | five | Maximum 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_kwargs | Optional[Dict[str, Any]] | None | A 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.
| Parameter | Type | Default | Description |
|---|---|---|---|
prompt | str | The string prompt to use for text generation. | |
system_prompt | Optional[str] | None | 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_callback | Optional[StreamingCallbackT] | None | A callback function called when a new token is received from the stream. |
generation_kwargs | Optional[Dict[str, Any]] | None | Additional keyword arguments for text generation. These parameters override the parameters passed in the __init__ method. For more details, see OpenAI documentation. |
Was this page helpful?