Lesson 2 of 3 Building Generative Applications with Foundry

Calling Language, Code and Multimodal Models with the Responses API

This lesson explains how an application calls deployed models through the Responses API, including structured outputs and streaming, and how calling patterns differ between model types.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the elements of a Responses API request.
  • Use structured outputs to constrain a response to a JSON Schema.
  • Identify the inputs and settings of each model type.

The Responses API

Once a model is deployed, an application calls it through an API (application programming interface): it sends a request containing the input and settings, and receives the model's output. The Responses API is the recommended API for this in new applications, and it also supports tools and agents. A request names the deployment in model, provides input (a string or a list of messages and content parts) and optional instructions, tools, output format and parameters. The response contains output items: messages, function calls, tool results and reasoning summaries; output_text collects the text. Responses can be stored and chained with previous_response_id, or attached to a conversation. The older Chat Completions API remains supported.

A model keeps no memory between requests, so a multi-turn exchange works only if the earlier turns are supplied again with each new request. The Responses API offers three ways of doing this, and they differ in who keeps the history.

With application-managed history, the application sends every earlier message in input on each request. This gives full control over what is included, but requests grow with every turn.

With previous_response_id, the service already holds the earlier response, so the application sends only the new input and the ID, which keeps requests small. The chain is a simple sequence of responses.

A conversation is a durable object that holds messages, tool calls and results across many requests and can be shared with agents. It suits assistants that users return to over time.

Whichever approach is used, all earlier turns still count as input tokens when they are sent to the model, so long conversations cost more per request. Summarizing or trimming old turns limits that growth, and stored responses and conversations are subject to the organization's data retention rules like any other stored data.

The following diagram compares what the application sends on the third turn of a conversation.

What the application sends on the third turn under each approach. With application-managed history it sends every earlier message. With previous_response_id it sends the new input and the ID of the stored response. With a conversation it sends the new input and the conversation ID, and the service holds the messages, tool calls and results.
The approaches differ in who keeps the history, not in what the model receives: earlier turns reach the model and count as input tokens in all three.

The Continue in code option of a deployment's playground generates a starting point. The following screenshot shows the Python sample for chat-primary: an OpenAI client pointed at the resource's /openai/v1 endpoint, authenticated with a Microsoft Entra ID token provider, and a responses.create call that passes the deployment name as the model.

Call chat-primary in code dialog with a Python sample using DefaultAzureCredential and client.responses.create
The sample uses Entra ID authentication; the API key field is hidden in this screenshot.

Structured outputs

A model's natural output is prose. When code must read the result, for example to store a category in a database or branch on a priority, prose is unreliable: a model asked to "return JSON with category and priority" usually does so, but occasionally adds an explanation, renames a field or omits one, and the parser fails. At thousands of requests a day, even a 1% failure rate becomes a steady stream of errors.

Structured outputs solve this by constraining generation to a JSON Schema. The service only allows tokens that keep the output valid against the schema, so every response contains the required fields with the declared types, and enum fields contain only the listed values. In the Responses API, the schema is passed in the text format setting with strict enabled; with the OpenAI Python library, a Pydantic model can be passed to responses.parse, which builds the schema and returns a parsed object. The older JSON mode only guarantees syntactically valid JSON, not a particular shape, so structured outputs are preferred.

Structured outputs guarantee the shape of the answer, not the correctness of the values in it. A correctly typed field can still hold the wrong value, so important values are validated in code and the extraction is evaluated against labelled examples. In the following example, the Ticket class defines the schema, and output_parsed returns a Ticket instance.

from pydantic import BaseModel

class Ticket(BaseModel):
    category: str
    priority: int
    summary: str

parsed = openai.responses.parse(
    model="chat-primary",
    instructions="Classify the support email. Priority 1 is highest.",
    input=email_text,
    text_format=Ticket,
)
ticket = parsed.output_parsed     # a Ticket instance
Scenario questions

Call models reliably

Exam skills 2.1.1 · 2.1.5

Practise Responses API options for reliable results.

Log in to try this activity Interactive activities are free with an account.

Model types

The model families, described in the lesson on selecting models for each task, differ in what they are good at. They also differ in how they are called. All model types deployed from Foundry Models use the same endpoint and client, but each accepts different inputs and settings, and using a setting that a model type does not support causes an error or wasted cost. The following paragraphs describe how each type is called.

A general LLM takes text or messages and is tuned with temperature or top_p and an output limit.

A reasoning model takes the same input but does not accept temperature. It is tuned with reasoning effort instead, and its hidden reasoning tokens are billed as output.

A small model such as Phi is called like an LLM, but its quality must be confirmed on the organization's own data before it replaces a larger model.

A code model receives code and instructions and is usually combined with structured outputs or tools. Its output must be reviewed and tested like any other code.

A multimodal model receives images as content parts alongside the text. The image detail setting trades accuracy on fine detail against the extra tokens each image costs.

An embedding model does not generate text at all. It receives a batch of strings and returns vectors, and the same model must be used for documents and queries because vectors from different models cannot be compared.

The following table summarizes these differences.

Model typeInputKey settingsWatch for
General LLMText, messagestemperature or top_p, max_output_tokensOutput length and cost
Reasoning modelText, messagesreasoning effort, max_output_tokensNo temperature; reasoning tokens billed
Small model (for example Phi)TextSame as LLMValidate quality on your data
Code modelCode and instructionsStructured output or toolsReview and test generated code
Multimodal modelText plus input_image (URL or data URI)Image detail low, high or autoImage tokens add cost
Embedding modelText (batch of strings)dimensions (for text-embedding-3)Same model for documents and queries

Streaming (stream=True) returns events as tokens are generated, which reduces perceived latency in chat interfaces. Multimodal input sends image parts next to the text in the same user message.

Worked example

Reliable extraction

A developer extracts order numbers and delivery dates from customer emails with a prompt that asks for JSON. About 3% of responses cannot be parsed, and some contain extra fields.

Use structured outputs with a strict schema (or responses.parse with a Pydantic model), which guarantees the shape, and lower the temperature for consistency. Adding "Return only JSON" to the prompt or switching to JSON mode would reduce but not eliminate shape errors.

Check yourself

In a Responses API call, what value goes in the model parameter?

Show answer

The deployment name, not the underlying model's name.

Exam Tips

Remember for the exam

  • Responses API: model is the deployment name; output_text collects the text.
  • Guaranteed schema: structured outputs (strict JSON Schema or responses.parse).
  • Reasoning models use reasoning effort, not temperature.
  • Use the same embedding model for indexing and querying.