Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

AI & ML · Const

Temperature

Temperature controls sampling randomness in a completion. Values from 0 to 2 trade determinism for variety: lower is more focused, higher is more random.

How it works

Temperature controls randomness during sampling. Lower values make output more focused and deterministic, while higher values increase variation; it accepts values from 0 to 2.

Syntax

temperature: number | null

Parameters

NameTypeDescription
temperaturenumber or nullSampling temperature, from 0 to 2. Higher values increase randomness; lower values make output more focused and deterministic.

Returns

The generated completion, including its text and associated metadata.

Key points

  • Set temperature to a number from 0 through 2.
  • Higher settings produce more varied and less predictable output.
  • Lower settings favor focused, repeatable-looking output.
  • Temperature and top_p are alternative sampling controls; normally adjust only one.

Common mistakes

  • Avoid changing temperature and top_p together; tune one sampling control at a time.

Example: Use a low temperature for focused completions

from openai import OpenAI

client = OpenAI()
response = client.completions.create(
    model="gpt-3.5-turbo-instruct",
    prompt="Write a haiku about winter.",
    temperature=0.2,
    max_tokens=30,
)
print(response.choices[0].text)

This runnable Python example requests a focused completion by setting temperature to 0.2.

When to use it

Focused generation

Generate stable, focused answers or code with less variation.

Creative generation

Increase creative variation in prose or brainstorming output.

Sampling control

Tune output randomness while leaving top_p unchanged.

  • top_p
  • max_tokens
  • model
  • prompt

Source: Completions | OpenAI API Reference. Checked 2026-10-15.