I am trying a simple code in langGraph where the llm has to call wikipedia search tool when needed. However, it returns the following results: The resp: content='<function=search-wikipedia{"query": "Palmyra"}</function>' additional_kwargs={} response_metadata={'token_usage': {'completion_tokens': 17, 'prompt_tokens': 247, 'total_tokens': 264, 'completion_time': 0.061818182, 'prompt_time': 0.034164437, 'queue_time': 0.666112673, 'total_time': 0.095982619}, 'model_name': 'llama-3.3-70b-versatile', 'system_fingerprint': 'fp_3884478861', 'finish_reason': 'stop', 'logprobs': None} id='run-cd05b340-619d-4672-bdda-578f095985ad-0' usage_metadata={'input_tokens': 247, 'output_tokens': 17, 'total_tokens': 264} Tool Calls [] Here is my code: @tool("search-wikipedia", args_schema=SearchWikiInput) def get_res(query: str) -> str: print(bcolors.WARNING + f'Using the wikipedia tool:...\n' + bcolors.ENDC) search_query = model.invoke([SystemMessage(content=f'''Convert the user queryinto search keyword in wikipedia, if the query is a simple greeting, return an empty response only, else return only and only the keyword: {query}''')]).content if search_query == "": return {'wiki_content': ''} pages = search_for_page(query) content = get_wiki_content(pages) if not content: return "No results found." formatted_results = [] for page in content: formatted_results.append( f"Title: {page.title}\n" f"Summary: {page.summary[:800]}...\n" f"URL: {page.url}\n" ) results = '\n\n'.join(formatted_results) print(bcolors.WARNING + f'Returned Results from wikipedia' + bcolors.ENDC) return {'wiki_content': results} tools = [get_res] tools_dict = {tool.name: tool for tool in tools} model = ChatGroq(model_name=model_name, temperature=0.1).bind_tools(tools) def tools_node(state: State): """Tool call node that executes the tools based on the plan.""" outputs = [] print('Hello from the tools node') for tool_call in state['messages'][-1].tool_calls: tool_results = tools_dict[tool_call['name']].invoke(tool_call['args']) outputs.append( ToolMessage( content=json.dumps(tool_results), name=tool_call['name'], tool_call_id=tool_call['id'] ) ) return {'messages': outputs} def agent_node(state: State): """Agent call node that uses the LLM with tools to answer the user query.""" agent_prompt = 'You are a helpful assistant, use the external tools only when needed' system_prompt = SystemMessage(content=agent_prompt) response = model.invoke([system_prompt] + state["messages"]) print(f'The resp: {response}') return {"messages": [response]} def should_continue(state:State): messages = state["messages"] last_message = messages[-1] print(f'Tool Calls {last_message.content}') if last_message.tool_calls: return 'continue' else: return 'END' workflow = StateGraph(State) workflow.add_node("agent", agent_node) workflow.add_node("tools", tools_node) workflow.add_edge(START,"agent") workflow.add_edge('tools',"agent") workflow.add_conditional_edges("agent",should_continue,{'continue':'tools','END':END}) graph = workflow.compile() I tried calling the model multiple times; in the first time it returns the results above, when calling it for the second time it executes the tool functions and sometimes it even works from the first time. Thanks in advance. Continue reading...