Tool call and structured ouput

I’m new to langchain. I’m using a model in Standalone mode.
In this i have defined a tools and output structures
I’m using Langchain V1

import os
import dotenv
from langchain.chat_models import init_chat_model
from langchain.tools import tool
from pydantic import BaseModel, Field

dotenv.load_dotenv()

class NumberOperation(BaseModel):
    """Number operation result."""
    input_number: int = Field(..., description="The input number")
    operation: str = Field(..., description="The operation performed")
    result: int = Field(..., description="The result of the operation")
    
@tool
def magnify_number(number: int) -> int:
    """Magnify a number """
    print("Magnifying number:", number)
    return number * 10

@tool
def maximize_number(number: int) -> int:
    """Maximize a number """
    print("Maximizing number:", number)
    return number * 100

model = init_chat_model(model_provider="openai", model=os.getenv("OPENAI_MODEL"), temperature=0.7)
model_with_tool = model.bind_tools([magnify_number,maximize_number])
model_with_weather_structure = model_with_tool.with_structured_output(NumberOperation)
response = model_with_weather_structure.invoke("Magnify the number 5")
print(response)
response = model_with_weather_structure.invoke("Maximize the number 36")
print(response)

i got the following output

input_number=5 operation='Magnify by factor of 2' result=10
input_number=36 operation='Clarification needed to maximize with rules.' result=0

this is not the expected output. what went wrong in this?

1 Like

The chat model itself will not execute the tools. It will just reply to you with a request to use the tools (see tool calling).

To execute the tools and return a response following your schema, use an agent— see docs here for agents + structured output.

3 Likes

Thanks @chester-lc