The Glean Chat API is a powerful tool for integrating conversational AI into your products. It allows you to create custom conversational interfaces that leverage Glean’s AI capabilities. This page provides examples of how to use the Chat API to integrate conversational AI into your products.
curl'https://<instance-name>-be.glean.com/rest/api/v1/chat'\-H'Content-Type: application/json'\-H'Authorization: Bearer <TOKEN>'\-d '{"stream": false,"messages":[{"author":"USER","fragments":[{"text":"What are the holidays this year?"}]}]}'
Note: The following example streams the output as it becomes available, whenever possible. To turn off streaming outputs, please set the stream field in the request body to False
As of 04/24/2025, the /chat API response has been changed. The previous way of processing the response message stream has been deprecated.
As in-line citations (citations inside the fragments) do not contain startIndex or endIndex, you might still find this deprecated function useful
as a way to get the endIndex of each citation (startIndex is no longer populated). Please also note that this function may return the same
citation multiple times, although each duplicated citation should have a different endIndex.
import requestsimport jsondefprocess_message_fragment(message): message_type = message['messageType'] fragments = message.get('fragments',[]) message_fragment_str =''if message_type =='CONTENT':for fragment in fragments:# Fragments may contain text or citations. This is because citations are outputted# inline, along with the response. There will only be one citation per fragment.if'text'in fragment: text = fragment.get('text','')print(text, end='', flush=True)elif'citation'in fragment: citation = fragment.get('citation',{}) sourceDocument = citation.get('sourceDocument',{})if sourceDocument: source = citation['sourceDocument']print(f'(Source: Document title - {source["title"]}, url: {source["url"]})') sourcePerson = citation.get('sourcePerson',{})if sourcePerson: source = citation['sourcePerson']print(f'(Source: Person name - {source["name"]})')# For citations from Web Search, Databricks Genie, JIRA JQL, Salesforce SOQL. sourceFile = citation.get('sourceFile',{})if sourceFile: source = citation['sourceFile']print(f'(Source: File name - {source["name"]})')defprocess_response_message_stream(response):for line in response.iter_lines():if line: line_json = json.loads(line) messages = line_json.get('messages',[])for message in messages: process_message_fragment(message)defmain(): url ='https://<instance-name>-be.glean.com/rest/api/v1/chat' headers ={'Content-Type':'application/json','Authorization':'Bearer <TOKEN>'} data ={'stream':True,# Set to False to toggle off streaming mode'messages':[{'author':'USER','fragments':[{'text':'What are the holidays this year?'}]}],}try:with requests.post(url, headers=headers, json=data, stream=True)as response:if response.status_code ==200: process_response_message_stream(response)else:print(f'Status code: {response.status_code}, error: {response.text}') exit(1)except requests.exceptions.RequestException as e:print(f'Request Exception: {str(e)}') exit(1)if __name__ =='__main__': main()