Repository: langchain
Issue: Tool Calling Fails with Error with Azure Search for Agentic RAG
Labels: 🤖:bug

### Checked other resources

- [X] I added a very descriptive title to this issue.
- [X] I searched the [LangGraph](https://langchain-ai.github.io/langgraph/)/LangChain documentation with the integrated search.
- [X] I used the GitHub search to find a similar question and didn't find it.
- [X] I am sure that this is a bug in LangGraph/LangChain rather than my code.
- [X] I am sure this is better as an issue [rather than a GitHub discussion](https://github.com/langchain-ai/langgraph/discussions/new/choose), since this is a LangGraph bug and not a design question.

### Example Code

```python
**Tool creation**    
embeddings = AzureOpenAIEmbeddings(model=embedding_model_name,azure_deployment="text-embedding-3-large")

# Create the vector store
vector_store = AzureSearch(
    azure_search_endpoint=azure_search_endpoint,
    azure_search_key=azure_search_key,
    index_name=search_index_name,
    embedding_function=embeddings.embed_query,
)

retriever_ = vector_store.as_retriever() // Return AzureSearch Object
retrieve_policy_document = create_retriever_tool(
retriever=retriever_,
name="retriever",
description="Search and Return  tool for HR Related questions and Leave Policy in NewYork.",)

** Tool Biding to Agent **

vector_store = create_vector_store()
self.retrieval_tool = create_vector_store_tool(vector_store)

# Bind the retriever tool to the LLM
self.model_tool = self.llm.bind_tools([self.retrieval_tool])
response = self.model_tool.invoke(messages)

** Langgraph workflow ***

workflow = StateGraph(AgentState)

# Add nodes to the workflow
workflow.add_node("agent", self.agent)
retrieve_node = ToolNode([self.retrieval_tool])
workflow.add_node("retrieve", retrieve_node)
workflow.add_node("rewrite", self.rewrite)
workflow.add_node("generate", self.generate)

# Define the workflow edges
workflow.add_edge(START, "agent")

# Conditional edges based on the agent's decision
workflow.add_conditional_edges(
    "agent",
    tools_condition,
    {
        "tools": "retrieve",
        END: END,
    },
)

# Conditional edges after retrieval
workflow.add_conditional_edges(
    "retrieve",
    self.grade_documents,
    {
        "generate": "generate",
        "rewrite": "rewrite",
    },
)

workflow.add_edge("generate", END)
workflow.add_edge("rewrite", "agent")
```


### Error Message and Stack Trace (if applicable)

```shell
Does not perform Tool Call for the above code example, instead gets response from LLM.

When i change tool call to below. Return with invalid tool call errors.

retrieve_policy_document = create_retriever_tool(
retriever=retriever_,
name="retriever_policy_documents",
description="Search and Return  tool for HR Related questions and Leave Policy in NewYork.",
)

================================== Ai Message ==================================
Invalid Tool Calls:
  python (call_MnPE9xzRf5aedp2j0z6kIA6w)
 Call ID: call_MnPE9xzRf5aedp2j0z6kIA6w
  Error: Function python arguments:

// Here's an example implementation of the retriever_policy_documents function
// that searches for HR related questions and leave policies in New York.

functions.retriever_policy_documents = ({ query }) => {
  const documents = [
    {
      title: "Extended Leave Policy",
      content: "Employees may request an extended leave of absence for medical reasons or to care for a family member. The leave may be unpaid, but job protection is provided under the Family and Medical Leave Act (FMLA).",
      category: "Leave Policy",
      location: "New York"
    },
    {
      title: "FMLA",
      content: "The Family and Medical Leave Act (FMLA) provides eligible employees with up to 12 weeks of unpaid leave per year for certain family and medical reasons. To be eligible, employees must have worked for their employer for at least 12 months and have worked at least 1,250 hours during the previous 12 months.",
      category: "HR Policy",
      location: "New York"
    },
    {
      title: "Disability Leave",
      content: "Employees who are unable to work due to a disability may be eligible for disability leave under the Americans with Disabilities Act (ADA) and the New York State Human Rights Law. The length of the leave and the amount of pay may vary depending on the circumstances.",
      category: "Leave Policy",
      location: "New York"
    }
  ];

  const results = documents.filter(doc => {
    const queryWords = query.toLowerCase().split(" ");
    const docWords = doc.title.toLowerCase().split(" ");
    return queryWords.every(word => docWords.includes(word));
  });

  return results;
};

are not valid JSON. Received JSONDecodeError Expecting value: line 1 column 1 (char 0)
For troubleshooting, visit: https://python.langchain.com/docs/troubleshooting/errors/OUTPUT_PARSING_FAILURE
  Args:
    // Here's an example implementation of the retriever_policy_documents function
// that searches for HR related questions and leave policies in New York.

functions.retriever_policy_documents = ({ query }) => {
  const documents = [
    {
      title: "Extended Leave Policy",
      content: "Employees may request an extended leave of absence for medical reasons or to care for a family member. The leave may be unpaid, but job protection is provided under the Family and Medical Leave Act (FMLA).",
      category: "Leave Policy",
      location: "New York"
    },
    {
      title: "FMLA",
      content: "The Family and Medical Leave Act (FMLA) provides eligible employees with up to 12 weeks of unpaid leave per year for certain family and medical reasons. To be eligible, employees must have worked for their employer for at least 12 months and have worked at least 1,250 hours during the previous 12 months.",
      category: "HR Policy",
      location: "New York"
    },
    {
      title: "Disability Leave",
      content: "Employees who are unable to work due to a disability may be eligible for disability leave under the Americans with Disabilities Act (ADA) and the New York State Human Rights Law. The length of the leave and the amount of pay may vary depending on the circumstances.",
      category: "Leave Policy",
      location: "New York"
    }
  ];

  const results = documents.filter(doc => {
    const queryWords = query.toLowerCase().split(" ");
    const docWords = doc.title.toLowerCase().split(" ");
    return queryWords.every(word => docWords.includes(word));
  });

  return results;
};
None
```


### Description

Here are the libraries.
```
langchain                                0.3.6
langchain-anthropic                      0.2.3
langchain-cli                            0.0.31
langchain-community                      0.3.4
langchain-core                           0.3.14
langchain-openai                         0.2.3
langchain-text-splitters                 0.3.0
langchainhub                             0.1.21
langdetect                               1.0.9
langgraph                                0.2.38
langgraph-checkpoint                     2.0.1
langgraph-checkpoint-postgres            2.0.1
langgraph-sdk                            0.1.33
```

Tool Calling does not work with Azure Open AI & Azure Search combination. My use cases requires providing responses based on existing documentation which loaded to Azure Search. I attempted to perform Agentic RAG follow examples..

https://langchain-ai.github.io/langgraph/tutorials/rag/langgraph_agentic_rag/

Retriever step wan not called instead getting response from LLM directly.

How do i enable debugging in LangGraph to show execution of steps and input and output from LangGraph workflow execution ?

Full code can be found below.

https://github.com/kishorekkota/agentic_app/blob/main/agentic_rag/azure_search.py

https://github.com/kishorekkota/agentic_app/blob/main/agentic_rag/rag_assistant.py








### System Info

System Information
------------------
> OS:  Darwin
> OS Version:  Darwin Kernel Version 24.1.0: Thu Oct 10 21:03:15 PDT 2024; root:xnu-11215.41.3~2/RELEASE_ARM64_T6000
> Python Version:  3.12.7 (main, Oct  1 2024, 02:05:46) [Clang 16.0.0 (clang-1600.0.26.3)]

Package Information
-------------------
> langchain_core: 0.3.14
> langchain: 0.3.6
> langchain_community: 0.3.4
> langsmith: 0.1.133
> langchain_anthropic: 0.2.3
> langchain_cli: 0.0.31
> langchain_openai: 0.2.3
> langchain_text_splitters: 0.3.0
> langchainhub: 0.1.21
> langgraph: 0.2.38
> langserve: 0.3.0

Other Dependencies
------------------
> aiohttp: 3.10.9
> anthropic: 0.36.0
> async-timeout: Installed. No version info available.
> dataclasses-json: 0.6.7
> defusedxml: 0.7.1
> fastapi: 0.115.2
> gitpython: 3.1.43
> gritql: 0.1.5
> httpx: 0.27.2
> httpx-sse: 0.4.0
> jsonpatch: 1.33
> langgraph-checkpoint: 2.0.1
> langgraph-sdk: 0.1.33
> langserve[all]: Installed. No version info available.
> numpy: 1.26.4
> openai: 1.52.0
> orjson: 3.10.7
> packaging: 24.1
> pydantic: 2.9.2
> pydantic-settings: 2.5.2
> PyYAML: 6.0.2
> requests: 2.32.3
> requests-toolbelt: 1.0.0
> SQLAlchemy: 2.0.35
> sse-starlette: 1.8.2
> tenacity: 8.5.0
> tiktoken: 0.8.0
> tomlkit: 0.12.5
> typer[all]: Installed. No version info available.
> types-requests: 2.32.0.20241016
> typing-extensions: 4.12.2
> uvicorn: 0.23.2