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?

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.

Thanks @chester-lc

As chester-lc said model itself will not execute the tools. Here you can use the following code to make tool calls without agent.

for res in response:
    for tool_call in res.tool_calls:
        print("Tool called:", tool_call['name'])
        print("Tool arguments:", tool_call['args'])
        tool_response = None
        if tool_call['name']== "magnify_number":
            tool_response = magnify_number.invoke(tool_call)
        elif tool_call['name']== "maximize_number":
            tool_response = maximize_number.invoke(tool_call)
        print("Tool response:", tool_response)

pass the tool response to model to the structured output

@yugandharreddy thanks for the code

@yugandharreddy This helped me in writing my own custom state modifier