Repository: langchain
Issue: Callback error when using a `return_direct` tool and `RunnableWithMessageHistory`
Labels: 🤖:bug

### Checked other resources

- [x] I added a very descriptive title to this issue.
- [x] I searched the 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 LangChain rather than my code.
- [x] The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

### Example Code

```py
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_core.prompts import ChatPromptTemplate
from langchain.agents.agent import AgentExecutor
from langchain.agents import create_tool_calling_agent

model = ChatGoogleGenerativeAI(model="gemini-2.0-flash-exp")
prompt = ChatPromptTemplate.from_messages(
        [
            ("system", PROMPT_TEMPLATE),
            ("placeholder", "{chat_history}"),
            ("human", "{input}"),
            ("placeholder", "{agent_scratchpad}"),
        ]
    )

class StructuredResponseTool(BaseTool):
    """
    Tool that can be used by the LLM to provide a structured response to the user.
    """
    name: str = "respond_to_user"
    description: str = (
        "Always use this tool to provide response to the user, and to indicate whether the conversation should end. "
        "The `message` content should be what you would normally respond with in a conversation. "
        "The `end_conversation` flag should be set to True if the conversation should end after this response."
    )
    args_schema: type[BaseModel] = LLMResponse

    return_direct: bool = True  # Causes the tool result to be returned directly to the user

    def _run(self, message: str, end_conversation: bool) -> LLMResponse:
        return LLMResponse(message=message, end_conversation=end_conversation)


tools = [StructuredResponseTool()]

agent = create_tool_calling_agent(model, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
# Use a single message history (no need for multiple threads)
message_history = ChatMessageHistory()

def get_session_history(_) -> BaseChatMessageHistory:
    return message_history

agent_with_chat_history = RunnableWithMessageHistory(
    agent_executor,
    get_session_history,
    input_messages_key="input",
    history_messages_key="chat_history",
)

response = agent_with_chat_history.invoke({"input": "Hello"})
```

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

`Error in RootListenersTracer.on_chain_end callback: ValueError("Expected str, BaseMessage, List[BaseMessage], or Tuple[BaseMessage]. Got message='Hello, welcome to Le Bistro! Do you have any dietary restrictions?' end_conversation=False.")`

### Description

The `RootListenersTracer.on_chain_end callback` reports an error when using a tool with `return_direct=True` that returns a non-`str` value, and the `RunnableWithMessageHistory` wrapper.

 This is due to how it [validates the message history](https://github.com/langchain-ai/langchain/blob/dbb6b7b103d9c32cea46d3848839a4c9cbb493c3/libs/core/langchain_core/runnables/history.py#L486)



### System Info

System Information
------------------
> OS:  Darwin
> OS Version:  Darwin Kernel Version 23.6.0: Mon Jul 29 21:14:30 PDT 2024; root:xnu-10063.141.2~1/RELEASE_ARM64_T6000
> Python Version:  3.11.9 (main, Jun 20 2024, 15:53:48) [Clang 15.0.0 (clang-1500.3.9.4)]

Package Information
-------------------
> langchain_core: 0.3.31
> langchain: 0.3.15
> langchain_community: 0.3.15
> langsmith: 0.2.11
> langchain_anthropic: 0.3.3
> langchain_google_genai: 2.0.9
> langchain_google_vertexai: 2.0.7
> langchain_openai: 0.3.1
> langchain_text_splitters: 0.3.5
> langgraph_sdk: 0.1.51

Optional packages not installed
-------------------------------
> langserve

Other Dependencies
------------------
> aiohttp: 3.11.11
> anthropic: 0.43.1
> anthropic[vertexai]: Installed. No version info available.
> async-timeout: Installed. No version info available.
> dataclasses-json: 0.6.7
> defusedxml: 0.7.1
> filetype: 1.2.0
> google-cloud-aiplatform: 1.77.0
> google-cloud-storage: 2.19.0
> google-generativeai: 0.8.4
> httpx: 0.27.2
> httpx-sse: 0.4.0
> jsonpatch: 1.33
> langchain-mistralai: Installed. No version info available.
> langsmith-pyo3: Installed. No version info available.
> numpy: 1.26.4
> openai: 1.59.9
> orjson: 3.10.15
> packaging: 24.2
> pydantic: 2.10.5
> pydantic-settings: 2.7.1
> PyYAML: 6.0.2
> requests: 2.32.3
> requests-toolbelt: 1.0.0
> SQLAlchemy: 2.0.37
> tenacity: 9.0.0
> tiktoken: 0.8.0
> typing-extensions: 4.12.2
> zstandard: Installed. No version info available