errors.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. from __future__ import annotations
  2. from collections.abc import Sequence
  3. from enum import Enum
  4. from typing import Any
  5. from warnings import warn
  6. # EmptyChannelError is re-exported from langgraph.channels.base
  7. from langgraph.checkpoint.base import EmptyChannelError # noqa: F401
  8. from typing_extensions import deprecated
  9. from langgraph.types import Command, Interrupt
  10. from langgraph.warnings import LangGraphDeprecatedSinceV10
  11. __all__ = (
  12. "EmptyChannelError",
  13. "ErrorCode",
  14. "GraphRecursionError",
  15. "InvalidUpdateError",
  16. "GraphBubbleUp",
  17. "GraphInterrupt",
  18. "NodeInterrupt",
  19. "ParentCommand",
  20. "EmptyInputError",
  21. "TaskNotFound",
  22. )
  23. class ErrorCode(Enum):
  24. GRAPH_RECURSION_LIMIT = "GRAPH_RECURSION_LIMIT"
  25. INVALID_CONCURRENT_GRAPH_UPDATE = "INVALID_CONCURRENT_GRAPH_UPDATE"
  26. INVALID_GRAPH_NODE_RETURN_VALUE = "INVALID_GRAPH_NODE_RETURN_VALUE"
  27. MULTIPLE_SUBGRAPHS = "MULTIPLE_SUBGRAPHS"
  28. INVALID_CHAT_HISTORY = "INVALID_CHAT_HISTORY"
  29. def create_error_message(*, message: str, error_code: ErrorCode) -> str:
  30. return (
  31. f"{message}\n"
  32. "For troubleshooting, visit: https://docs.langchain.com/oss/python/langgraph/"
  33. f"errors/{error_code.value}"
  34. )
  35. class GraphRecursionError(RecursionError):
  36. """Raised when the graph has exhausted the maximum number of steps.
  37. This prevents infinite loops. To increase the maximum number of steps,
  38. run your graph with a config specifying a higher `recursion_limit`.
  39. Troubleshooting guides:
  40. - [`GRAPH_RECURSION_LIMIT`](https://docs.langchain.com/oss/python/langgraph/GRAPH_RECURSION_LIMIT)
  41. Examples:
  42. graph = builder.compile()
  43. graph.invoke(
  44. {"messages": [("user", "Hello, world!")]},
  45. # The config is the second positional argument
  46. {"recursion_limit": 1000},
  47. )
  48. """
  49. pass
  50. class InvalidUpdateError(Exception):
  51. """Raised when attempting to update a channel with an invalid set of updates.
  52. Troubleshooting guides:
  53. - [`INVALID_CONCURRENT_GRAPH_UPDATE`](https://docs.langchain.com/oss/python/langgraph/INVALID_CONCURRENT_GRAPH_UPDATE)
  54. - [`INVALID_GRAPH_NODE_RETURN_VALUE`](https://docs.langchain.com/oss/python/langgraph/INVALID_GRAPH_NODE_RETURN_VALUE)
  55. """
  56. pass
  57. class GraphBubbleUp(Exception):
  58. pass
  59. class GraphInterrupt(GraphBubbleUp):
  60. """Raised when a subgraph is interrupted, suppressed by the root graph.
  61. Never raised directly, or surfaced to the user."""
  62. def __init__(self, interrupts: Sequence[Interrupt] = ()) -> None:
  63. super().__init__(interrupts)
  64. @deprecated(
  65. "NodeInterrupt is deprecated. Please use [`interrupt`][langgraph.types.interrupt] instead.",
  66. category=None,
  67. )
  68. class NodeInterrupt(GraphInterrupt):
  69. """Raised by a node to interrupt execution."""
  70. def __init__(self, value: Any, id: str | None = None) -> None:
  71. warn(
  72. "NodeInterrupt is deprecated. Please use `langgraph.types.interrupt` instead.",
  73. LangGraphDeprecatedSinceV10,
  74. stacklevel=2,
  75. )
  76. if id is None:
  77. super().__init__([Interrupt(value=value)])
  78. else:
  79. super().__init__([Interrupt(value=value, id=id)])
  80. class ParentCommand(GraphBubbleUp):
  81. args: tuple[Command]
  82. def __init__(self, command: Command) -> None:
  83. super().__init__(command)
  84. class EmptyInputError(Exception):
  85. """Raised when graph receives an empty input."""
  86. pass
  87. class TaskNotFound(Exception):
  88. """Raised when the executor is unable to find a task (for distributed mode)."""
  89. pass