Skip to content

Commit a71b555

Browse files
committed
Make create_database_tool return structured response
1 parent 1d57d90 commit a71b555

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

samples/adk-sql-agent/sql_agent/tools.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class SqlRunResult(TypedDict):
3434
"""The rows returned by the SQL query"""
3535

3636
@tracer.start_as_current_span("create_database")
37-
def create_database_tool(tool_context: ToolContext) -> None:
37+
def create_database_tool(tool_context: ToolContext) -> dict[str, Any]:
3838
"""Creates a temporary file in the /tmp directory to hold an ephemeral
3939
sqlite3 database if a database is not found for the current session.
4040
"""
@@ -44,11 +44,16 @@ def create_database_tool(tool_context: ToolContext) -> None:
4444
# current session.
4545
# See https://google.github.io/adk-docs/sessions/state/.
4646
tool_context.state[SESSION_DB_KEY] = path
47+
return {"resp": "Created an ephemeral database"}
48+
return {"resp": f"Skipping database creation, {tool_context.state[SESSION_DB_KEY]} already exists"}
4749

4850
@tracer.start_as_current_span("run_sql")
4951
def run_sql_tool(sql_query: str, tool_context: ToolContext) -> dict[str, Any]:
5052
"""Runs a SQLite query. The SQL query can be DDL or DML. Returns the rows if it's a SELECT query."""
51-
current_session_db_path = tool_context.state[SESSION_DB_KEY]
53+
current_session_db_path = tool_context.state.get(SESSION_DB_KEY)
54+
if current_session_db_path is None:
55+
return {"error": "Failed to find a database fo this session"}
56+
5257
with sqlite3.connect(current_session_db_path) as db:
5358
try:
5459
cursor = db.cursor()

0 commit comments

Comments
 (0)