POST /ai/v1/grounded/responses. Gloo finds relevant passages in your uploaded content and adds them to the model’s context before it answers: Retrieval-Augmented Generation (RAG), in the same request and response shape as the Responses API.
You’ll scope retrieval to your publisher, check whether an answer was actually grounded, handle questions your content doesn’t cover, and show readers the sources behind an answer.
Want the concepts first? Read the Grounded Responses guide. For every parameter and response field, see the Grounded Responses API reference.
Grounding never fails the request. When nothing relevant is found, the model still answers, from its own knowledge, and the response reports
sources_returned: false. That flag is the one signal that separates an answer from your content from one that isn’t. This tutorial checks it on every call.Prerequisites
You need:- A Gloo AI Studio account
- Your API key from the API Keys page, exported as
GLOO_API_KEY - A publisher with content uploaded. See Manage Publishers and the Upload Files tutorial
- Authentication setup: complete the Authentication Tutorial first
- Python 3.9+ with
requests(pip install requests), or Node.js 18+ (for built-infetch)
What You’ll Build
A helper,answer(question), that returns one of four results your interface can show safely:
You’ll build it in five steps, add streaming in Step 6, then put it together in Complete Code.
Step 1: Your First Grounded Request
A grounded request is a Responses request plus grounding parameters. Setrag_publisher to your publisher name.
Understanding the Response
The response is standard Responses output, a typedoutput[] array plus usage, with one addition: a top-level sources_returned boolean.
Step 2: Scope Retrieval to Your Content
Three parameters decide which content reaches the model:
For an assistant that should speak only from your material, always set
rag_publisher.
Step 3: Tell the Model How to Use the Sources
The grounded endpoint adds the passages; yourinstructions decide how the model treats them. Encode a few rules from The Prompt Contract:
"instructions": INSTRUCTIONS to every request. The quotation rule matters for Step 5: it means anything in quotation marks claims to be verbatim, so your code can check it.
Step 4: Handle Questions Your Content Doesn’t Cover
When retrieval finds nothing, because the publisher has no matching content or no passage clearscertainty_threshold, the model is told no publisher sources were retrieved and still answers. The answer can be fluent and confident, and it isn’t from your content.
Check sources_returned before anything else, and give the user an honest message instead of the ungrounded answer:
sources_returned: true tells you sources reached the model, not that the answer reflects them. A model can still produce a quotation or a title no source contains, a hallucination. Step 5 adds that check.Step 5: Show Sources and Verify Quotations
Citations build trust only if they’re real. Grounded Responses returnssources_returned but not the passages themselves, so fetch them from the Search API with the same question, against the same publisher. Use the results two ways:
- Show them as “Sources from our library”: each result has
properties.item_titleand the passage text inproperties.snippet. - Verify against them: every quotation in the answer should appear in one of the passages.
Search uses your publisher’s tenant name, which has spaces removed (
Example Publisher becomes ExamplePublisher). The surest way to get it is GET /ai/v1/data/tenant/names. The collection is always GlooProd.properties.item_url when it isn’t empty.
Step 6: Stream a Grounded Answer
Set"stream": true to receive the same Server-Sent Events as the Responses API. The grounding outcome is known before the first token, so it arrives as the X-Sources-Returned response header instead of a body field. Read it before you render anything:
Complete Code
Theanswer() helper from What You’ll Build: check grounding first, verify quotations against passages from Search, widen both once, and decline rather than show a quotation that couldn’t be confirmed. An answer with no quotations, with a quotation too short to check (under 12 characters, such as a single quoted word), or with an unclosed quotation comes back as unverified: show it with its sources, but don’t label it as checked. An answer with quotations but no Search passages to check them against comes back as unsupported. It uses output_text, INSTRUCTIONS, grounded, search_passages, quoted_spans, balanced_quotes and unverified_quotes from Steps 1 to 5; put them in one file with this code.
Troubleshooting
sources_returned is always false
: Check that rag_publisher matches your publisher name exactly, as shown in Manage Publishers, and that your uploads have finished processing. Then try a question your documents clearly answer. If you set certainty_threshold, remove it to fall back to the platform default.
Answers draw on content that isn’t yours
: rag_publisher is missing, so the request grounded in the default GlooGrounded dataset. Set it on every request.
Follow-up questions like “tell me more” come back ungrounded
: Retrieval keys on the latest user message, and a short follow-up has little to search on. Carry the original question’s terms into the follow-up, or reuse the sources from the earlier turn. See Multi-Turn Conversations.
Search returns 400 or 403
: tenant must be the normalized tenant name with spaces removed, which you can get from GET /ai/v1/data/tenant/names, and collection must be GlooProd. You can only search your own publisher’s tenant.
A correct quotation is flagged as unconfirmed
: The Search results may not include the passage the model quoted. Raise the Search limit, or move to Grounded Completions to check against the citation snippets the model was given.
Every streamed answer is treated as ungrounded
: Compare X-Sources-Returned without regard to case (true / True), and read it from the response headers, not the event stream.
Prompts are large and costly
: Retrieved passages count as input tokens. Lower sources_limit; it’s your main lever over the cost of each grounded request.
Next Steps
Grounded Responses guide
Parameters, streaming, reasoning and how it compares to Grounded Completions
Grounded Responses API reference
Every request parameter and response field
Building Trustworthy Grounded Applications
Verification, prompt contracts, multi-turn handling and evaluation
Handling Streaming Failures
Retry ceilings, continuation and partial-output UX
Upload Files tutorial
Get more of your content into your publisher
Build with Direct Responses
The same request shape, without retrieval

