How to Beginner · 3 min read

How to make AI return a list

Quick answer
To make AI return a list, explicitly instruct it in the prompt to provide output as a numbered or bulleted list using phrases like "List the items as:" or "Provide a numbered list of:". Use clear formatting instructions and examples to guide models such as gpt-4o or claude-3-5-sonnet-20241022.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the openai Python package and set your API key as an environment variable for secure access.

bash
pip install openai>=1.0

Step by step

Use explicit prompt instructions to get a list output. Below is a complete example using gpt-4o with the OpenAI Python SDK v1+.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

prompt = "List the top 5 programming languages in 2026 as a numbered list."

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": prompt}]
)

print(response.choices[0].message.content)
output
1. Python
2. JavaScript
3. Rust
4. Go
5. TypeScript

Common variations

You can customize list style (bullets, numbers), use other models like claude-3-5-sonnet-20241022, or implement streaming for real-time list generation.

python
import os
import anthropic

client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

system_prompt = "You are a helpful assistant that returns lists as bullet points."
user_prompt = "Provide a bulleted list of 3 popular AI frameworks in 2026."

message = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=200,
    system=system_prompt,
    messages=[{"role": "user", "content": user_prompt}]
)

print(message.content[0].text)
output
- TensorFlow
- PyTorch
- JAX

Troubleshooting

If the AI returns paragraphs instead of lists, clarify the prompt by adding explicit formatting instructions like "Use a numbered list with each item on a new line." Also, try adding example outputs in the prompt to guide the model.

python
prompt = "List 3 cloud providers in 2026 as a numbered list, each item on a new line, like:\n1. Example\n2. Example\n3. Example"

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": prompt}]
)

print(response.choices[0].message.content)
output
1. AWS
2. Google Cloud
3. Microsoft Azure

Key Takeaways

  • Always explicitly instruct the AI to format output as a list using clear phrases.
  • Provide examples or formatting templates in the prompt to improve list accuracy.
  • Use numbered or bulleted list keywords depending on desired output style.
  • Different models like gpt-4o and claude-3-5-sonnet-20241022 support list outputs well with proper prompting.
  • If output is not a list, refine prompt clarity and add example formatting.
Verified 2026-04 · gpt-4o, claude-3-5-sonnet-20241022
Verify ↗