3
3
from __future__ import annotations
4
4
5
5
import re
6
- from typing import Any , Dict , Iterable , List , Literal , Optional , Sequence , Union
6
+ from typing import Any , Dict , Iterable , List , Literal , Optional , Sequence , Tuple , Union
7
7
8
8
import sqlalchemy
9
9
from langchain_core ._api import deprecated
@@ -544,11 +544,22 @@ def run(
544
544
* ,
545
545
parameters : Optional [Dict [str , Any ]] = None ,
546
546
execution_options : Optional [Dict [str , Any ]] = None ,
547
- ) -> Union [str , Sequence [Dict [str , Any ]], Result [Any ]]:
548
- """Execute a SQL command and return a string representing the results.
547
+ response_format : Literal ["content" , "content_and_artifact" ] = "content" ,
548
+ ) -> Union [str , Sequence [Dict [str , Any ]], Result [Any ], Tuple [str , Any ]]:
549
+ """Execute a SQL command and return its result.
549
550
550
- If the statement returns rows, a string of the results is returned.
551
- If the statement returns no rows, an empty string is returned.
551
+ Args:
552
+ command: The SQL command to execute.
553
+ fetch: The number of rows to fetch. Can be "one", "all", or "cursor".
554
+ include_columns: Whether to include column names in the result.
555
+ parameters: A dictionary of parameters to pass to the SQL command.
556
+ execution_options: A dictionary of execution options for the engine.
557
+ response_format: The format of the response. Defaults to "content".
558
+
559
+ Returns:
560
+ A string representation of the results, or a cursor object. If
561
+ `response_format` is 'content_and_artifact', returns a tuple of
562
+ (string_representation, processed_result_list).
552
563
"""
553
564
result = self ._execute (
554
565
command , fetch , parameters = parameters , execution_options = execution_options
@@ -568,10 +579,12 @@ def run(
568
579
if not include_columns :
569
580
res = [tuple (row .values ()) for row in res ] # type: ignore[misc]
570
581
571
- if not res :
572
- return ""
582
+ string_result = "" if not res else str (res )
583
+
584
+ if response_format == "content_and_artifact" :
585
+ return string_result , res
573
586
else :
574
- return str ( res )
587
+ return string_result
575
588
576
589
def get_table_info_no_throw (self , table_names : Optional [List [str ]] = None ) -> str :
577
590
"""Get information about specified tables.
0 commit comments