ToolNode output is broken for non-english #213

Closed
opened 2026-02-20 17:31:57 -05:00 by yindo · 2 comments
Owner

Originally created by @sangmandu on GitHub (Aug 27, 2024).

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangGraph/LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangGraph/LangChain rather than my code.
  • I am sure this is better as an issue rather than a GitHub discussion, since this is a LangGraph bug and not a design question.

Example Code

langgraph.prebuilt.tool_node.py

def str_output(output: Any) -> str:
    if isinstance(output, str):
        return output
    else:
        try:
            return json.dumps(output)
        except Exception:
            return str(output)

Error Message and Stack Trace (if applicable)

No response

Description

I found the tool node output is encoded.
And when I check _run_one and _arun_one function, they use str_output function.
And I checked the arg, the input is alreadt encoded before return str or json.dumps.
I don't know about that well...

this is not completely suitable code but I can solve my problem temporarily

def str_output(output: Any) -> str:
    return json.dumps(json.loads(output), ensure_ascii=False)

And I think the origin problem presents here: langchain_core.tools.base

def _stringify(content: Any) -> str:
    try:
        return json.dumps(content)
    except Exception:
        return str(content)

they just got a dumps without ensure_ascii=False.

Thank you for reading and I am forwarding to fixing this quickly

System Info

Just simple error

Originally created by @sangmandu on GitHub (Aug 27, 2024). ### 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 langgraph.prebuilt.tool_node.py def str_output(output: Any) -> str: if isinstance(output, str): return output else: try: return json.dumps(output) except Exception: return str(output) ``` ### Error Message and Stack Trace (if applicable) _No response_ ### Description I found the tool node output is encoded. And when I check _run_one and _arun_one function, they use str_output function. And I checked the arg, the input is alreadt encoded before return str or json.dumps. I don't know about that well... this is not completely suitable code but I can solve my problem temporarily ```python def str_output(output: Any) -> str: return json.dumps(json.loads(output), ensure_ascii=False) ``` And I think the origin problem presents here: langchain_core.tools.base ```python def _stringify(content: Any) -> str: try: return json.dumps(content) except Exception: return str(content) ``` they just got a dumps without ensure_ascii=False. Thank you for reading and I am forwarding to fixing this quickly ### System Info Just simple error
yindo closed this issue 2026-02-20 17:31:58 -05:00
Author
Owner

@gbaian10 commented on GitHub (Aug 28, 2024):

Test Case

from enum import Enum

from dotenv import load_dotenv
from langchain.pydantic_v1 import BaseModel
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langgraph.graph import MessagesState, StateGraph
from langgraph.prebuilt import ToolNode

load_dotenv()


class Day(str, Enum):
    MONDAY = "星期一"
    TUESDAY = "星期二"
    WEDNESDAY = "水曜日"
    THURSDAY = "목요일"
    FRIDAY = "星期五"
    SATURDAY = "星期六"
    SUNDAY = "星期日"


class Week(BaseModel):
    """choose days"""

    days: list[Day]


@tool(args_schema=Week)
def get_day_list(days: list[Day]) -> list[Day]:
    return days


tools = [get_day_list]
model = ChatOpenAI(model="gpt-4o-mini").bind_tools(tools)

workflow = StateGraph(MessagesState)
workflow.add_node("enter", lambda state: {"messages": model.invoke(state["messages"])})
workflow.add_node("tool", ToolNode(tools))

workflow.set_entry_point("enter")
workflow.add_edge("enter", "tool")
workflow.set_finish_point("tool")
app = workflow.compile()
outputs = app.invoke(
    {"messages": "我星期一和 Friday and 水曜日 和 목요일 have free time"}
)
print(outputs["messages"][-1].content)

Output

["\u661f\u671f\u4e00", "\u661f\u671f\u4e94", "\u6c34\u66dc\u65e5", "\ubaa9\uc694\uc77c"]

Expected Output.

["星期一", "星期五", "水曜日", "목요일"]
@gbaian10 commented on GitHub (Aug 28, 2024): Test Case ```py from enum import Enum from dotenv import load_dotenv from langchain.pydantic_v1 import BaseModel from langchain_core.tools import tool from langchain_openai import ChatOpenAI from langgraph.graph import MessagesState, StateGraph from langgraph.prebuilt import ToolNode load_dotenv() class Day(str, Enum): MONDAY = "星期一" TUESDAY = "星期二" WEDNESDAY = "水曜日" THURSDAY = "목요일" FRIDAY = "星期五" SATURDAY = "星期六" SUNDAY = "星期日" class Week(BaseModel): """choose days""" days: list[Day] @tool(args_schema=Week) def get_day_list(days: list[Day]) -> list[Day]: return days tools = [get_day_list] model = ChatOpenAI(model="gpt-4o-mini").bind_tools(tools) workflow = StateGraph(MessagesState) workflow.add_node("enter", lambda state: {"messages": model.invoke(state["messages"])}) workflow.add_node("tool", ToolNode(tools)) workflow.set_entry_point("enter") workflow.add_edge("enter", "tool") workflow.set_finish_point("tool") app = workflow.compile() outputs = app.invoke( {"messages": "我星期一和 Friday and 水曜日 和 목요일 have free time"} ) print(outputs["messages"][-1].content) ``` Output ```shell ["\u661f\u671f\u4e00", "\u661f\u671f\u4e94", "\u6c34\u66dc\u65e5", "\ubaa9\uc694\uc77c"] ``` Expected Output. ```shell ["星期一", "星期五", "水曜日", "목요일"] ```
Author
Owner

@eyurtsev commented on GitHub (Sep 11, 2024):

Will merge a fix in core, but users should be able to return strings directly from the tool to avoid relying on any auto-conversion of Enum values to strings (or anything more complex).

@eyurtsev commented on GitHub (Sep 11, 2024): Will merge a fix in core, but users should be able to return strings directly from the tool to avoid relying on any auto-conversion of Enum values to strings (or anything more complex).
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langgraph#213