Saltar al contenido

Lección 4 · Generating Text

Instructions and Message Roles

Use developer, user and assistant roles and the instructions parameter to steer OpenAI models, write clear prompts and keep untrusted text separate.

A good request tells the model two different things. One is how your application should behave: its job, its limits and its output style. The other is what this particular user wants right now. The API gives you separate places for them. Using those places well makes replies more consistent and your program easier to maintain.

This lesson covers message roles, the instructions parameter, and a simple structure for writing instructions that work.

Input can be a list of messages

So far input was a plain string. It can also be a list of messages, each with a role and content.

Python
# Read-only
response = client.responses.create(
    model="gpt-6.2-sol",
    input=[
        {"role": "developer",
         "content": "You help customers of a bicycle shop. Answer in at most three sentences."},
        {"role": "user", "content": "My chain keeps slipping. What should I check?"},
    ],
)
print(response.output_text)

The three roles you will use are these.

Role Who writes it Purpose
developer You, the application author Rules, tone, task and limits
user The person using your app The request itself
assistant The model Earlier replies in the conversation

Developer messages take priority over user messages. In older code you may see the role system used for a similar purpose.

The instructions parameter

There is also a top-level instructions argument.

Python
# Read-only
response = client.responses.create(
    model="gpt-6.2-sol",
    instructions="Answer in formal English.",
    input="how do i reset my password",
)

instructions applies only to the request that includes it. If you chain responses on the server, as shown in the next lesson, instructions from an earlier turn are not carried over. A developer message at the start of the conversation stays part of the history.

There is also a cost detail. For GPT-6.2 models, OpenAI's documentation says that top-level instructions are placed after the input, so they do not benefit from prompt caching. It suggests putting long, stable rules in the first developer message and keeping instructions for guidance that applies to one request. This kind of detail can change between model families, so check the current guide. Tokens, costs and caching explains why it matters.

Write instructions that are easy to follow

A model cannot read your mind. It only has the text you send. A useful developer message usually answers five questions.

  1. Role and goal. What is the assistant for?
  2. Audience. Who will read the reply?
  3. Rules. What must it always or never do?
  4. Format. How long, what structure, which language?
  5. Escape route. What should it do when it does not know or the request is out of scope?
TEXT
You are the help assistant for a small bicycle shop.
Readers are adult customers who are not mechanics.

Rules:
- Use only the shop information below for prices and opening hours.
- If the answer is not in that information, say you do not know and
  suggest contacting the shop.
- Do not give instructions for repairs that affect brakes. Recommend a visit.

Format: plain text, at most 80 words, in the customer's language.

The escape route matters most. Without it, a model may fill a gap with a confident guess. Read more in prompt engineering and especially clear and specific instructions.

Keep data separate from rules

Often you need to include material that is not an instruction: a customer email, a product page, a log file. Mark where it starts and ends, and say what it is. This helps the model, and it helps people who read your prompts.

This runnable helper builds the message list. It also removes closing tags from the untrusted text, so the text cannot pretend to end its own section.

Python Assemble role-based input safely

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Delimiters and priority rules reduce the risk of prompt injection, where text in your data tries to give the model new orders. They do not remove it. Never rely on a prompt alone to protect money, private data or important actions. Enforce those limits in code. See prompt injection and security.

Do not put secrets in prompts

Anything in your developer message could end up in a reply if a user asks cleverly enough. Do not include passwords, keys, private notes about customers or anything you would not want users to see. Write the instructions as if they might become public.

Reusable prompts live in your code

Some older material shows prompt templates saved in the OpenAI dashboard and called by ID. OpenAI announced the end of these reusable prompt objects, with a shutdown date of November 30, 2026, and told developers to move prompts into their code. That is good practice anyway: prompts in your repository get code review, version history and tests.

A simple pattern is one module or folder for prompts, each with a name and a version.

Python
SUPPORT_SUMMARY_V3 = """You summarize customer emails for the support team.
Reply with two short sentences."""

When you change a prompt, run the same test examples before and after. Testing and evaluating shows how.

Improve instructions with evidence

Your first version will miss cases. Run it on a handful of realistic messages, including rude, vague and off-topic ones, and read every reply. When something is wrong, ask why. Was the rule missing, unclear, or in conflict with another rule? Fix that one thing, then run all the examples again, because a new rule can break an old behavior.

Prefer positive, specific statements. "Answer in at most 80 words" is easier to follow and test than "do not be too long." Explain the reason for a rule when it is not obvious: a model can apply "no repair advice for brakes, because a mistake could cause injury" more sensibly than a bare ban. Avoid piles of capital letters and threats. They add noise and rarely express the real requirement.

If you need a particular output shape for a program, do not describe it only in words. Use a schema, as the structured outputs lessons show.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Which role is meant for your application's rules?

    Developer messages hold the application author's rules and take priority over user messages.
  2. 2. What happens to top-level instructions when you chain a new request to an earlier response?

    The instructions parameter applies to the request that supplies it.
  3. 3. What is a useful escape route in instructions?

    A clear fallback reduces confident guessing.
  4. 4. How much protection do delimiters give against prompt injection?

    They make the boundary clearer, but they are not a security control by themselves.

Summary

  • Input can be a list of messages with developer, user and assistant roles.
  • instructions applies to one request; check how your model family caches it.
  • State goal, audience, rules, format and what to do when unsure.
  • Mark untrusted text as data, and enforce critical limits in code.
  • Keep prompts in version control and keep secrets out of them.

Next: Conversation State

Tu progreso se guarda en este navegador. No necesitas una cuenta.