0% found this document useful (0 votes)
43 views10 pages

Text Generation and Prompting - OpenAI API

The document provides an overview of text generation and prompting using the OpenAI API, detailing how to effectively prompt models to generate various types of text responses. It discusses model selection, prompt engineering techniques, and the importance of message roles and formatting for optimal results. Additionally, it highlights best practices for using reasoning and GPT models, as well as the significance of including relevant context and managing the context window during text generation.

Uploaded by

vosohac516
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views10 pages

Text Generation and Prompting - OpenAI API

The document provides an overview of text generation and prompting using the OpenAI API, detailing how to effectively prompt models to generate various types of text responses. It discusses model selection, prompt engineering techniques, and the importance of message roles and formatting for optimal results. Additionally, it highlights best practices for using reasoning and GPT models, as well as the significance of including relevant context and managing the context window during text generation.

Uploaded by

vosohac516
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

26/07/2025, 07:45 Text generation and prompting - OpenAI API

Text generation and prompting


Text input and output
Learn how to prompt a model to generate text.
Choosing a model

With the OpenAI API, you can use a large language model to Prompt engineering
generate text from a prompt, as you might using ChatGPT. Message roles and
Models can generate almost any kind of text response—like instruction following
code, mathematical equations, structured JSON data, or
Reusable prompts
human-like prose.
Message formatting
Here's a simple example using the Chat Completions API.
Few-shot learning

Generate text from a simple prompt Include relevant context

from openai import OpenAI Prompt GPT-4.1 models


client = OpenAI()
Prompt reasoning models

completion = client.chat.completions.create( Next steps


model="gpt-4.1",
messages=[
{
"role": "user",
"content": "Write a one-sentence bedtim
}
]
)

print(completion.choices[0].message.content)

An array of content generated by the model is in the


choices property of the response. In this simple example,
we have just one output which looks like this:

[
{
"index": 0,
"message": {
"role": "assistant",
"content": "Under the soft glow of the
"refusal": null
},
"logprobs": null,
"finish_reason": "stop"
}
]
https://platform.openai.com/docs/guides/text?prompt-example=code 1/10
26/07/2025, 07:45 Text generation and prompting - OpenAI API

In addition to plain text, you can also have the model return
structured data in JSON format - this feature is called
Structured Outputs.

Choosing a model
A key choice to make when generating content through the
API is which model you want to use - the model parameter
of the code samples above.
You can find a full listing of available models here. Here are a
few factors to consider when choosing a model for text
generation.

Reasoning models generate an internal chain of thought


to analyze the input prompt, and excel at understanding
complex tasks and multi-step planning. They are also
generally slower and more expensive to use than GPT
models.
GPT models are fast, cost-efficient, and highly intelligent,
but benefit from more explicit instructions around how to
accomplish tasks.
Large and small (mini or nano) models offer trade-offs
for speed, cost, and intelligence. Large models are more
effective at understanding prompts and solving problems
across domains, while small models are generally faster
and cheaper to use.

When in doubt, gpt-4.1 offers a solid combination of


intelligence, speed, and cost effectiveness.

Prompt engineering
Prompt engineering is the process of writing effective
instructions for a model, such that it consistently generates
content that meets your requirements.

Because the content generated from a model is non-


deterministic, it is a combination of art and science to build a
prompt that will generate content in the format you want.

https://platform.openai.com/docs/guides/text?prompt-example=code 2/10
26/07/2025, 07:45 Text generation and prompting - OpenAI API

However, there are a number of techniques and best practices


you can apply to consistently get good results from a model.

Some prompt engineering techniques will work with every


model, like using message roles. But different model types
(like reasoning versus GPT models) might need to be
prompted differently to produce the best results. Even different
snapshots of models within the same family could produce
different results. So as you are building more complex
applications, we strongly recommend that you:

Pin your production applications to specific


model snapshots (like gpt-4.1-2025-04-14 for
example) to ensure consistent behavior.
Build evals that will measure the behavior of your
prompts, so that you can monitor the performance of your
prompts as you iterate on them, or when you change and
upgrade model versions.

Now, let's examine some tools and techniques available to you


to construct prompts.

Message roles and instruction


following
You can provide instructions (prompts) to the model with
differing levels of authority using message roles.

Generate text with messages using different roles

from openai import OpenAI


client = OpenAI()

completion = client.chat.completions.create(
model="gpt-4.1",
messages=[
{
"role": "developer",
"content": "Talk like a pirate."
},
{
"role": "user",
"content": "Are semicolons optional in
}
]

https://platform.openai.com/docs/guides/text?prompt-example=code 3/10
26/07/2025, 07:45 Text generation and prompting - OpenAI API

print(completion.choices[0].message.content)

The OpenAI model spec describes how our models give


different levels of priority to messages with different roles.

DEVELOPER USER ASSISTANT

developer messages user messages are Messages


are instructions instructions provided by an generated by the
provided by the end user, prioritized model have
application developer, behind developer messages. the assistant rol
prioritized ahead
of user messages.

A multi-turn conversation may consist of several messages of


these types, along with other content types provided by both
you and the model. Learn more about
managing conversation state here.

You could think about developer and user messages like


a function and its arguments in a programming language.

developer messages provide the system's rules and


business logic, like a function definition.
user messages provide inputs and configuration to
which the developer message instructions are applied,
like arguments to a function.

Reusable prompts
In the OpenAI dashboard, you can develop reusable prompts
that you can use in API requests, rather than specifying the
content of prompts in code. This way, you can more easily
build and evaluate your prompts, and deploy improved
versions of your prompts without changing your integration
code.

Reusable prompts are currently supported in the


Responses API only. They are not available in the Chat
Completions API.

https://platform.openai.com/docs/guides/text?prompt-example=code 4/10
26/07/2025, 07:45 Text generation and prompting - OpenAI API

Message formatting with


Markdown and XML
When writing developer and user messages, you can
help the model understand logical boundaries of your prompt
and context data using a combination of Markdown formatting
and XML tags.

Markdown headers and lists can be helpful to mark distinct


sections of a prompt, and to communicate hierarchy to the
model. They can also potentially make your prompts more
readable during development. XML tags can help delineate
where one piece of content (like a supporting document used
for reference) begins and ends. XML attributes can also be
used to define metadata about content in the prompt that can
be referenced by your instructions.

In general, a developer message will contain the following


sections, usually in this order (though the exact optimal
content and order may vary by which model you are using):

Identity: Describe the purpose, communication style, and


high-level goals of the assistant.
Instructions: Provide guidance to the model on how to
generate the response you want. What rules should it
follow? What should the model do, and what should the
model never do? This section could contain many
subsections as relevant for your use case, like how the
model should call custom functions.
Examples: Provide examples of possible inputs, along
with the desired output from the model.
Context: Give the model any additional information it
might need to generate a response, like
private/proprietary data outside its training data, or any
other data you know will be particularly relevant. This
content is usually best positioned near the end of your
prompt, as you may include different context for different
generation requests.

Below is an example of using Markdown and XML tags to


construct a developer message with distinct sections and
supporting examples.

https://platform.openai.com/docs/guides/text?prompt-example=code 5/10
26/07/2025, 07:45 Text generation and prompting - OpenAI API

Example prompt API request

Send a prompt to generate code through the API

from openai import OpenAI


client = OpenAI()

with open("prompt.txt", "r", encoding="utf-8") as f


instructions = f.read()

response = client.responses.create(
model="gpt-4.1",
instructions=instructions,
input="How would I declare a variable for a las
)

print(response.output_text)

Save on cost and latency with prompt caching

When constructing a message, you should try and keep


content that you expect to use over and over in your API
requests at the beginning of your prompt, and among the first
API parameters you pass in the JSON request body to
Chat Completions or Responses. This enables you to
maximize cost and latency savings from prompt caching.

Few-shot learning
Few-shot learning lets you steer a large language model
toward a new task by including a handful of input/output
examples in the prompt, rather than fine-tuning the model. The
model implicitly "picks up" the pattern from those examples
and applies it to a prompt. When providing examples, try to
show a diverse range of possible inputs with the desired
outputs.

Typically, you will provide examples as part of a developer


message in your API request. Here's an example
developer message containing examples that show a
model how to classify positive or negative customer service
reviews.

https://platform.openai.com/docs/guides/text?prompt-example=code 6/10
26/07/2025, 07:45 Text generation and prompting - OpenAI API

# Identity

You are a helpful assistant that labels short produ


Positive, Negative, or Neutral.

# Instructions

* Only output a single word in your response with n


or commentary.
* Your response should only be one of the words "Po
"Neutral" depending on the sentiment of the produ

# Examples

<product_review id="example-1">
I absolutely love this headphones — sound quality i
</product_review>

<assistant_response id="example-1">
Positive
</assistant_response>

<product_review id="example-2">
Battery life is okay, but the ear pads feel cheap.
</product_review>

<assistant_response id="example-2">
Neutral
</assistant_response>

<product_review id="example-3">
Terrible customer service, I'll never buy from them
</product_review>

<assistant_response id="example-3">
Negative
</assistant_response>

Include relevant context


information
It is often useful to include additional context information the
model can use to generate a response within the prompt you
give the model. There are a few common reasons why you
might do this:

https://platform.openai.com/docs/guides/text?prompt-example=code 7/10
26/07/2025, 07:45 Text generation and prompting - OpenAI API

To give the model access to proprietary data, or any other


data outside the data set the model was trained on.
To constrain the model's response to a specific set of
resources that you have determined will be most
beneficial.

The technique of adding additional relevant context to the


model generation request is sometimes called retrieval-
augmented generation (RAG). You can add additional
context to the prompt in many different ways, from querying a
vector database and including the text you get back into a
prompt, or by using OpenAI's built-in file search tool to
generate content based on uploaded documents.

Planning for the context window

Models can only handle so much data within the context they
consider during a generation request. This memory limit is
called a context window, which is defined in terms of tokens
(chunks of data you pass in, from text to images).

Models have different context window sizes from the low 100k
range up to one million tokens for newer GPT-4.1 models.
Refer to the model docs for specific context window sizes per
model.

Prompting GPT-4.1 models


GPT models like gpt-4.1 benefit from precise instructions
that explicitly provide the logic and data required to complete
the task in the prompt. GPT-4.1 in particular is highly steerable
and responsive to well-specified prompts. To get the most out
of GPT-4.1, refer to the prompting guide in the cookbook.

GPT-4.1 prompting guide


Get the most out of prompting GPT-4.1 with the tips and
tricks in this prompting guide, extracted from real-world use
cases and practical experience.

GPT-4.1 prompting best practices

https://platform.openai.com/docs/guides/text?prompt-example=code 8/10
26/07/2025, 07:45 Text generation and prompting - OpenAI API

While the cookbook has the best and most comprehensive


guidance for prompting this model, here are a few best
practices to keep in mind.

Building agentic workflows

Using long context

Prompting for chain of thought

Instruction following

Prompting reasoning models


There are some differences to consider when prompting a
reasoning model versus prompting a GPT model. Generally
speaking, reasoning models will provide better results on tasks
with only high-level guidance. This differs from GPT models,
which benefit from very precise instructions.

You could think about the difference between reasoning and


GPT models like this.

A reasoning model is like a senior co-worker. You can give


them a goal to achieve and trust them to work out the
details.
A GPT model is like a junior coworker. They'll perform
best with explicit instructions to create a specific output.

For more information on best practices when using reasoning


models, refer to this guide.

Next steps
Now that you known the basics of text inputs and outputs, you
might want to check out one of these resources next.

Build a prompt in the Playground


Use the Playground to develop and iterate on prompts.

Generate JSON data with Structured Outputs


https://platform.openai.com/docs/guides/text?prompt-example=code 9/10
26/07/2025, 07:45 Text generation and prompting - OpenAI API

Ensure JSON data emitted from a model conforms to a


JSON schema.

Full API reference


Check out all the options for text generation in the API
reference.

https://platform.openai.com/docs/guides/text?prompt-example=code 10/10

You might also like