untracked_value.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from __future__ import annotations
  2. from collections.abc import Sequence
  3. from typing import Any, Generic
  4. from typing_extensions import Self
  5. from langgraph._internal._typing import MISSING
  6. from langgraph.channels.base import BaseChannel, Value
  7. from langgraph.errors import EmptyChannelError, InvalidUpdateError
  8. __all__ = ("UntrackedValue",)
  9. class UntrackedValue(Generic[Value], BaseChannel[Value, Value, Value]):
  10. """Stores the last value received, never checkpointed."""
  11. __slots__ = ("value", "guard")
  12. guard: bool
  13. value: Value | Any
  14. def __init__(self, typ: type[Value], guard: bool = True) -> None:
  15. super().__init__(typ)
  16. self.guard = guard
  17. self.value = MISSING
  18. def __eq__(self, value: object) -> bool:
  19. return isinstance(value, UntrackedValue) and value.guard == self.guard
  20. @property
  21. def ValueType(self) -> type[Value]:
  22. """The type of the value stored in the channel."""
  23. return self.typ
  24. @property
  25. def UpdateType(self) -> type[Value]:
  26. """The type of the update received by the channel."""
  27. return self.typ
  28. def copy(self) -> Self:
  29. """Return a copy of the channel."""
  30. empty = self.__class__(self.typ, self.guard)
  31. empty.key = self.key
  32. empty.value = self.value
  33. return empty
  34. def checkpoint(self) -> Value | Any:
  35. return MISSING
  36. def from_checkpoint(self, checkpoint: Value) -> Self:
  37. empty = self.__class__(self.typ, self.guard)
  38. empty.key = self.key
  39. return empty
  40. def update(self, values: Sequence[Value]) -> bool:
  41. if len(values) == 0:
  42. return False
  43. if len(values) != 1 and self.guard:
  44. raise InvalidUpdateError(
  45. f"At key '{self.key}': UntrackedValue(guard=True) can receive only one value per step. Use guard=False if you want to store any one of multiple values."
  46. )
  47. self.value = values[-1]
  48. return True
  49. def get(self) -> Value:
  50. if self.value is MISSING:
  51. raise EmptyChannelError()
  52. return self.value
  53. def is_available(self) -> bool:
  54. return self.value is not MISSING