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
5
from pydantic import BaseModel , Field , model_validator
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."""
@@ -67,7 +68,7 @@ class DatabricksQueryTool(BaseTool):
67
68
default_schema : Optional [str ] = None
68
69
default_warehouse_id : Optional [str ] = None
69
70
70
- _workspace_client : Optional [WorkspaceClient ] = None
71
+ _workspace_client : Optional [" WorkspaceClient" ] = None
71
72
72
73
def __init__ (
73
74
self ,
@@ -89,8 +90,6 @@ def __init__(
89
90
self .default_catalog = default_catalog
90
91
self .default_schema = default_schema
91
92
self .default_warehouse_id = default_warehouse_id
92
-
93
- # Validate that Databricks credentials are available
94
93
self ._validate_credentials ()
95
94
96
95
def _validate_credentials (self ) -> None :
@@ -105,10 +104,16 @@ def _validate_credentials(self) -> None:
105
104
)
106
105
107
106
@property
108
- def workspace_client (self ) -> WorkspaceClient :
107
+ def workspace_client (self ) -> " WorkspaceClient" :
109
108
"""Get or create a Databricks WorkspaceClient instance."""
110
109
if self ._workspace_client is None :
111
- self ._workspace_client = WorkspaceClient ()
110
+ try :
111
+ from databricks .sdk import WorkspaceClient
112
+ self ._workspace_client = WorkspaceClient ()
113
+ except ImportError :
114
+ raise ImportError (
115
+ "`databricks-sdk` package not found, please run `uv add databricks-sdk`"
116
+ )
112
117
return self ._workspace_client
113
118
114
119
def _format_results (self , results : List [Dict [str , Any ]]) -> str :
@@ -733,4 +738,4 @@ def _run(
733
738
# Include more details in the error message to help with debugging
734
739
import traceback
735
740
error_details = traceback .format_exc ()
736
- return f"Error executing Databricks query: { str (e )} \n \n Details:\n { error_details } "
741
+ return f"Error executing Databricks query: { str (e )} \n \n Details:\n { error_details } "
0 commit comments