1
1
import os
2
- from typing import Any , Dict , List , Optional , Type , Union
2
+ from typing import TYPE_CHECKING , Any , Dict , List , Optional , Type , Union
3
3
4
4
from crewai .tools import BaseTool
5
- from databricks .sdk import WorkspaceClient
6
- from pydantic import BaseModel , Field , model_validator
5
+ from pydantic import BaseModel , Field , model_validator , ConfigDict , PrivateAttr
7
6
7
+ if TYPE_CHECKING :
8
+ from databricks .sdk import WorkspaceClient
8
9
9
10
class DatabricksQueryToolSchema (BaseModel ):
10
11
"""Input schema for DatabricksQueryTool."""
@@ -55,6 +56,10 @@ class DatabricksQueryTool(BaseTool):
55
56
>>> results = tool.run(query="SELECT * FROM my_table LIMIT 10")
56
57
"""
57
58
59
+ model_config = ConfigDict (
60
+ arbitrary_types_allowed = True , validate_assignment = True , frozen = False
61
+ )
62
+
58
63
name : str = "Databricks SQL Query"
59
64
description : str = (
60
65
"Execute SQL queries against Databricks workspace tables and return the results."
@@ -67,7 +72,7 @@ class DatabricksQueryTool(BaseTool):
67
72
default_schema : Optional [str ] = None
68
73
default_warehouse_id : Optional [str ] = None
69
74
70
- _workspace_client : Optional [WorkspaceClient ] = None
75
+ _workspace_client : Optional [" WorkspaceClient" ] = PrivateAttr ( None )
71
76
72
77
def __init__ (
73
78
self ,
@@ -89,8 +94,6 @@ def __init__(
89
94
self .default_catalog = default_catalog
90
95
self .default_schema = default_schema
91
96
self .default_warehouse_id = default_warehouse_id
92
-
93
- # Validate that Databricks credentials are available
94
97
self ._validate_credentials ()
95
98
96
99
def _validate_credentials (self ) -> None :
@@ -105,10 +108,16 @@ def _validate_credentials(self) -> None:
105
108
)
106
109
107
110
@property
108
- def workspace_client (self ) -> WorkspaceClient :
111
+ def workspace_client (self ) -> " WorkspaceClient" :
109
112
"""Get or create a Databricks WorkspaceClient instance."""
110
113
if self ._workspace_client is None :
111
- self ._workspace_client = WorkspaceClient ()
114
+ try :
115
+ from databricks .sdk import WorkspaceClient
116
+ self ._workspace_client = WorkspaceClient ()
117
+ except ImportError :
118
+ raise ImportError (
119
+ "`databricks-sdk` package not found, please run `uv add databricks-sdk`"
120
+ )
112
121
return self ._workspace_client
113
122
114
123
def _format_results (self , results : List [Dict [str , Any ]]) -> str :
@@ -733,4 +742,4 @@ def _run(
733
742
# Include more details in the error message to help with debugging
734
743
import traceback
735
744
error_details = traceback .format_exc ()
736
- return f"Error executing Databricks query: { str (e )} \n \n Details:\n { error_details } "
745
+ return f"Error executing Databricks query: { str (e )} \n \n Details:\n { error_details } "
0 commit comments