Skip to content

Conversation

Mateusz-Switala
Copy link
Collaborator

The goal of this PR is to add WatsonxSQLDatabase and WatsonxSQLDatabaseToolkit to langchain_ibm.

Example

from langchain_ibm.utilities.sql_database import WatsonxSQLDatabase

wx_sql_database = WatsonxSQLDatabase(connection_id=postgres_sql_connection_id,
                                     **credentials,
                                     schema="public"
                                     )

from langchain_ibm import ChatWatsonx

chat_llama = ChatWatsonx(
    watsonx_client=wx_sql_database.watsonx_client,
    model_id="mistralai/mistral-medium-2505",
    params={"max_new_tokens": 200},
)

from langchain_ibm.agent_toolkits.sql_toolkit import WatsonxSQLDatabaseToolkit


toolkit = WatsonxSQLDatabaseToolkit(db=wx_sql_database, llm=chat_llama)


from langchain import hub

prompt_template = hub.pull("langchain-ai/sql-agent-system-prompt")
system_message = prompt_template.format(dialect="PostgreSQL", top_k=5)

from langgraph.prebuilt import create_react_agent

agent_executor = create_react_agent(chat_llama, toolkit.get_tools(), prompt=system_message)


example_query = "What city in Japan has the largest population? How much it is?"

events = agent_executor.stream(
    {"messages": [("user", example_query)]},
    stream_mode="values",
)
for event in events:
    event["messages"][-1].pretty_print()

Copy link
Collaborator

@KarolZm KarolZm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Mateusz-Switala
Copy link
Collaborator Author

The changes includes also the new import path for WatsonxToolkit

from langchain_ibm.agent_toolkits.utility import WatsonxToolkit, WatsonxTool

except ValueError:
pass
else:
username = username or _from_env("WATSONX_USERNAME")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we improve style of these validations? Maybe using separate function

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I redesigned it a bit, now it should be fine.

Copy link
Collaborator

@MateuszOssGit MateuszOssGit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, please merge origin and resolve conflict

@Mateusz-Switala Mateusz-Switala merged commit d7bc0a5 into main Aug 22, 2025
15 checks passed
@Mateusz-Switala Mateusz-Switala deleted the feat-wx-sql-database branch August 28, 2025 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants