Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions libs/ibm/langchain_ibm/chat_models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""IBM watsonx.ai chat wrapper."""

import ast
import hashlib
import json
import logging
Expand Down Expand Up @@ -90,6 +91,35 @@
logger = logging.getLogger(__name__)


def normalize_tool_arguments(args_str: str) -> str:
"""Ensure arguments is always a proper JSON string.

Handles:
- JSON string
- Python dict string
- Extra wrapping quotes like '"{...}"'
Args:
args_str: tool call args_str.

Returns:
The LangChain tool call arguments args_str.
"""
# Try to parse as JSON
try:
parsed = json.loads(args_str)
except json.JSONDecodeError:
pass
else:
if isinstance(parsed, str):
json.loads(parsed)
return parsed
return args_str

# Try Python literal (e.g., "{'a': 1}")
obj: Any = ast.literal_eval(args_str)
return json.dumps(obj, ensure_ascii=False)


def _convert_dict_to_message(_dict: Mapping[str, Any], call_id: str) -> BaseMessage:
"""Convert a dictionary to a LangChain message.

Expand Down Expand Up @@ -117,6 +147,13 @@ def _convert_dict_to_message(_dict: Mapping[str, Any], call_id: str) -> BaseMess
if raw_tool_calls := _dict.get("tool_calls"):
additional_kwargs["tool_calls"] = raw_tool_calls
for raw_tool_call in raw_tool_calls:
## Code change to support langgraph with A2A and graph.astream.
if "function" in raw_tool_call:
func = raw_tool_call.get("function", {})
if "arguments" in func:
raw_args = raw_tool_call["function"]["arguments"]
json_args_str = normalize_tool_arguments(raw_args)
raw_tool_call["function"]["arguments"] = json_args_str
try:
tool_calls.append(parse_tool_call(raw_tool_call, return_id=True))
except Exception as e:
Expand Down
2 changes: 1 addition & 1 deletion libs/ibm/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "pdm.backend"

[project]
name = "langchain-ibm"
version = "0.3.19.dev1"
version = "0.3.20"
description = "An integration package connecting IBM watsonx.ai and LangChain"
authors = [{ name = "IBM" }]
license = { text = "MIT" }
Expand Down
2 changes: 1 addition & 1 deletion libs/ibm/tests/integration_tests/test_embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
WX_PROJECT_ID = os.environ.get("WATSONX_PROJECT_ID", "")

URL = "https://us-south.ml.cloud.ibm.com"
MODEL_ID = "ibm/slate-125m-english-rtrvr"
MODEL_ID = "ibm/slate-125m-english-rtrvr-v2"

DOCUMENTS = ["What is a generative ai?", "What is a loan and how does it works?"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

URL = "https://us-south.ml.cloud.ibm.com"

MODEL_ID = "ibm/granite-embedding-107m-multilingual"
MODEL_ID = "ibm/granite-embedding-278m-multilingual"


class TestWatsonxEmbeddingsStandard(EmbeddingsIntegrationTests):
Expand Down
4 changes: 2 additions & 2 deletions libs/ibm/tests/integration_tests/test_rerank.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

URL = "https://us-south.ml.cloud.ibm.com"

MODEL_ID = "ibm/slate-125m-english-rtrvr"
MODEL_ID = "ibm/slate-125m-english-rtrvr-v2"


def test_01_rerank_init() -> None:
Expand Down Expand Up @@ -47,7 +47,7 @@ def test_02_rerank_documents() -> None:


def test_02_rerank_documents_with_params() -> None:
params = RerankParameters(truncate_input_tokens=1)
params = RerankParameters(truncate_input_tokens=2)
test_documents = [
Document(page_content="This is a test document."),
]
Expand Down
2 changes: 1 addition & 1 deletion libs/ibm/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.