@@ -34,14 +34,16 @@ def ask_data_insights(
3434) -> Dict [str , Any ]:
3535 """Answers questions about structured data in BigQuery tables using natural language.
3636
37- This function takes auser 's question (which can include conversational
38- history for context) andreferences to specific BigQuery tables, and sends
37+ This function takes a user 's question (which can include conversational
38+ history for context) and references to specific BigQuery tables, and sends
3939 them to a stateless conversational API.
4040
4141 The API uses a GenAI agent to understand the question, generate and execute
4242 SQL queries and Python code, and formulate an answer. This function returns a
4343 detailed, sequential log of this entire process, which includes any generated
44- SQL or Python code, the data retrieved, and the final text answer.
44+ SQL or Python code, the data retrieved, and the final text answer. The final
45+ answer is always in plain text, as the underlying API is instructed not to
46+ generate any charts, graphs, images, or other visualizations.
4547
4648 Use this tool to perform data analysis, get insights, or answer complex
4749 questions about the contents of specific BigQuery tables.
@@ -123,9 +125,22 @@ def ask_data_insights(
123125 }
124126 ca_url = f"https://geminidataanalytics.googleapis.com/v1alpha/projects/{ project_id } /locations/{ location } :chat"
125127
128+ instructions = """**INSTRUCTIONS - FOLLOW THESE RULES:**
129+ 1. **CONTENT:** Your answer should present the supporting data and then provide a conclusion based on that data.
130+ 2. **OUTPUT FORMAT:** Your entire response MUST be in plain text format ONLY.
131+ 3. **NO CHARTS:** You are STRICTLY FORBIDDEN from generating any charts, graphs, images, or any other form of visualization.
132+ """
133+
134+ final_query_text = f"""
135+ { instructions }
136+
137+ **User Query and Context:**
138+ { user_query_with_context }
139+ """
140+
126141 ca_payload = {
127142 "project" : f"projects/{ project_id } " ,
128- "messages" : [{"userMessage" : {"text" : user_query_with_context }}],
143+ "messages" : [{"userMessage" : {"text" : final_query_text }}],
129144 "inlineContext" : {
130145 "datasourceReferences" : {
131146 "bq" : {"tableReferences" : table_references }
@@ -289,7 +304,7 @@ def _handle_data_response(
289304 schema = resp ["result" ]["schema" ]
290305 headers = [field .get ("name" ) for field in schema .get ("fields" , [])]
291306
292- all_rows = resp ["result" ][ "data" ]
307+ all_rows = resp ["result" ]. get ( "data" , [])
293308 total_rows = len (all_rows )
294309
295310 compact_rows = []
0 commit comments