cancel
Showing results for 
Search instead for 
Did you mean: 
Generative AI
Explore discussions on generative artificial intelligence techniques and applications within the Databricks Community. Share ideas, challenges, and breakthroughs in this cutting-edge field.
cancel
Showing results for 
Search instead for 
Did you mean: 

Genie Agent response Export to PDF via API

GunaR
Contributor

Hello,

Got a need to invoke the Genie Agent chat (in agent mode) via API, and on the request completion, export the agent response in PDF and share the exported PDF in email.  

Am already using 'Agent Mode Create Response' endpoint to invoke the chat programmatically. 
https://docs.databricks.com/api/genie/v1/agent-mode-create-response

What's the best way to export the response in PDF? I know there is no explicit endpoint to achieve the same. 

Thanks,
Guna. 

2 REPLIES 2

Ashwin_DSA
Databricks Employee
Databricks Employee

Hi @GunaR,

You are right. There is no API endpoint to export an Agent mode response as a PDF. The "Download PDF" button is a UI-only feature. But since the Agent Mode API provides a fully structured response, you can generate the PDF yourself on the client side.

When the SSE stream completes, the response.completed event contains all output items. The final report lives in the message output item. You can also retrieve it after the fact using the List conversation items endpoint:

GET /api/2.0/genie/agents/{agent_id}/conversations/{conversation_id}/items

Loop through the output and grab the message type item, that's your report text.

Once you have the report content, convert it to PDF using a Python library.weasyprint works well for this because the report is essentially markdown that you can render through HTML:

import markdown
from weasyprint import HTML

html = markdown.markdown(report_text, extensions=['tables'])
HTML(string=f"<html><body>{html}</body></html>").write_pdf("report.pdf")

If you want something lighter with no system dependencies, fpdf2 is a good alternative.

From there, attach the generated file and send it using smtplib, SendGrid, or whatever email service you already use.

Just remember that if you set enable_viz: true in your request, the API returns visualisation specs but not rendered images. You would need to render those separately (for example with matplotlib or plotly) before embedding them in the PDF.

Hope this helps.

If this answer resolves your question, could you mark it as “Accept as Solution”? That helps other users quickly find the correct fix.

Regards,
Ashwin | Delivery Solution Architect @ Databricks
Helping you build and scale the Data Intelligence Platform.
***Opinions are my own***

data_pulse
New Contributor II

@GunaR 

Adding to the above mentioned from @Ashwin_DSA, I tested this from Databricks and can confirm the client side PDF approach works.

A couple of useful observations from the test:

  • The completed Agent response can be extracted from the assistant message / output_text.
  • The returned report is already Markdown-like, including headings, bullets, inline code and source citation links.
  • Converting that Markdown → HTML → PDF with weasyprint preserved the formatting and clickable source links in my test.
  • One small issue I encountered was UTF-8 rendering (€, en-dash, etc.). Normalizing the response text and including <meta charset="utf-8"> fixed it.
html_body = markdown.markdown(report_text, extensions=["tables"])

HTML(
    string=f"""
    <html>
      <head><meta charset="utf-8"></head>
      <body>{html_body}</body>
    </html>
    """
).write_pdf("/tmp/genie_agent_report.pdf")

The flow I validated :

Agent Mode API → response.completed → output_text → Markdown → HTML → PDF

So while there is no dedicated PDF export API currently, generating it client side from the completed Agent response works well. The report content, headings, formatting, and source citation links were retained in the generated PDF.