Skip to content

Commit

Permalink
langchain_community.chat_models.oci_generative_ai: Fix a bug when usi…
Browse files Browse the repository at this point in the history
…ng optional parameters in tools (#28829)

When using tools with optional parameters, the parameter `type` is not
longer available since langchain update to 0.3 (because of the pydantic
upgrade?) and there is now an `anyOf` field instead. This results in the
`type` being `None` in the chat request for the tool parameter, and the
LLM call fails with the error:

```
oci.exceptions.ServiceError: {'target_service': 'generative_ai_inference', 
'status': 400, 'code': '400', 
'opc-request-id': '...', 
'message': 'Parameter definition must have a type.', 
'operation_name': 'chat'
...
}
```

Example code that fails:

```
from langchain_community.chat_models.oci_generative_ai import ChatOCIGenAI
from langchain_core.tools import tool
from typing import Optional

llm = ChatOCIGenAI(
        model_id="cohere.command-r-plus",
        service_endpoint="https://inference.generativeai.us-chicago-1.oci.oraclecloud.com",
        compartment_id="ocid1.compartment.oc1...",
        auth_profile="your_profile",
        auth_type="API_KEY",
        model_kwargs={"temperature": 0, "max_tokens": 3000},
)

@tool
def test(example: Optional[str] = None):
    """This is the tool to use to test things

    Args:
        example: example variable, defaults to None
    """
    return "this is a test"

llm_with_tools = llm.bind_tools([test])

result = llm_with_tools.invoke("can you make a test for g")
```

This PR sets the param type to `any` in that case, and fixes the
problem.

Co-authored-by: Erick Friis <[email protected]>
  • Loading branch information
streamnsight and efriis authored Dec 19, 2024
1 parent c3ccd93 commit c8db5a1
Showing 1 changed file with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"integer": "int",
"array": "List",
"object": "Dict",
"any": "any",
}


Expand Down Expand Up @@ -323,7 +324,7 @@ def convert_to_oci_tool(
if "description" in p_def
else "",
type=JSON_TO_PYTHON_TYPES.get(
p_def.get("type"), p_def.get("type")
p_def.get("type"), p_def.get("type", "any")
),
is_required="default" not in p_def,
)
Expand All @@ -342,7 +343,7 @@ def convert_to_oci_tool(
p_name: self.oci_tool_param(
description=p_def.get("description"),
type=JSON_TO_PYTHON_TYPES.get(
p_def.get("type"), p_def.get("type")
p_def.get("type"), p_def.get("type", "any")
),
is_required="default" not in p_def,
)
Expand All @@ -363,7 +364,7 @@ def convert_to_oci_tool(
p_name: self.oci_tool_param(
description=p_def.get("description"),
type=JSON_TO_PYTHON_TYPES.get(
p_def.get("type"), p_def.get("type")
p_def.get("type"), p_def.get("type", "any")
),
is_required=p_name in parameters.get("required", []),
)
Expand Down

0 comments on commit c8db5a1

Please sign in to comment.