Skip to main content

Structured Outputs and Tool Calling in TypeScript

Use schema validation for model proposals and a separate tool contract for live lookups.

A rectangular grid beside a mechanical lever with the headline “Shape or Tool Call?”.

A customer asks when their booking starts. The application needs an authorised lookup and a response the UI can reliably render. Tool calling handles the request to look something up. Structured output is useful when the final response must follow a format. Many applications need both, with independent checks around them.

Use one example end to end

The model proposes a lookupBooking call with a confirmation reference. The application validates the arguments, derives the current account from its authenticated session and calls the booking service. It returns only the fields the model needs. If there is no matching booking for this account, the tool returns a defined no-match result.

A final response type can express those outcomes directly:

TypeScript
type BookingAnswer =
  | { kind: "found"; startTime: string; timeZone: string }
  | { kind: "needs_reference"; question: string }
  | { kind: "not_found"; nextStep: string }
  | { kind: "unavailable"; nextStep: string };

This TypeScript union describes what the UI accepts. Use a runtime schema for untrusted inputs and the compatible JSON Schema format your provider expects. A TypeScript annotation alone can’t enforce anything at the network boundary. Handle refusal, an incomplete response or an unsupported structure where it enters your code.

Keep the execution contract separate

Treat generated tool arguments like any other external request. Check shape, length, account access and operation eligibility before acting. Set a reasonable tool budget and return a clear error when the lookup can’t run. A coherent final answer cannot compensate for a tool that queried data outside the user’s account.

A useful development fixture covers a real booking, a mistyped reference, a different account, and a temporarily unavailable lookup. Render every outcome on the page. This exercise can reveal that the UI also needs a timeout state, or that the phrase “not found” should be replaced with a next step.

If the task doesn’t need live data or external action, a structured response may be enough. If it only needs to execute a known operation with typed inputs, the caller can render the tool result directly. Select each mechanism for the boundary it serves.

Sources

Build a project

© 2026 All rights reserved.