Skip to main content
Helicone OSS LLM Observability

Use constrained outputs

Set clear boundaries and rules for the model's responses to improve accuracy, consistency, and utility
2 min read

What are constrained outputs#

Constrained outputs involve instructing the LLM to generate responses that adhere to specific limitations or formats. This could mean setting a word limit, specifying a response type (like "yes" or "no"), or requiring the output to match a particular pattern or structure.

How to implement constrained outputs#

  1. Set clear instructions: Be explicit about the constraints you want the model to follow.
  2. Specify the format: Define the exact format or pattern you expect.
  3. Limit the length: Set boundaries on the response length, such as word or character counts.
  4. Use controlled vocabularies: Restrict the model to use only certain words or phrases.
  5. Provide templates: Offer a template that the model should fill in.

Example#

Example 1: Binary Classification.

Limiting the response to 'Approved' or 'Denied' ensures consistency and simplifies automated processing.

Prompt:

Example 2: Short Answer Generation.

By specifying that the answer should be in one sentence, you prevent the model from providing overly long or off-topic responses. Prompt:

Prompt:

Example 3: Technical writing.

Setting an exact word limit challenges the model to be concise and focus on the most important information.

Prompt:

Why use constrained outputs#

  • Increase precision: Helps the model provide exactly what you need without unnecessary information.
  • Enhance consistency: Ensures uniformity across multiple outputs, which is crucial for tasks like data entry or form filling.
  • Simplify parsing: Makes it easier to programmatically process the responses.
  • Reduce errors: Minimizes the chance of irrelevant or incorrect information creeping into the output.