더북(TheBook)

다음 예제를 보겠습니다.

examples/8_exception.py

from langchain_core.tools import ToolException, tool

# 일반 예외 발생
@tool
def bad_summary(article: str) -> str:
    """글을 요약합니다."""
    if not article.strip():
        raise ValueError("내용이 비어 있습니다.")  
    return "..."

# ToolException 사용
@tool
def good_summary(article: str) -> str:
    """글을 요약합니다."""
    if not article.strip():
        raise ToolException(
            "내용이 비어 있습니다. 요약할 텍스트를 입력해 주세요."
        )
    return "..."