From cf67acb69978f82b79155b754238995635cc3cbe Mon Sep 17 00:00:00 2001 From: William FH <13333726+hinthornw@users.noreply.github.com> Date: Tue, 1 Oct 2024 17:23:44 -0700 Subject: [PATCH] Validate not empty (#1957) --- .../langgraph/store/base/__init__.py | 4 +++ libs/checkpoint/tests/test_store.py | 26 ++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/libs/checkpoint/langgraph/store/base/__init__.py b/libs/checkpoint/langgraph/store/base/__init__.py index d1ad914a6..6c8d65855 100644 --- a/libs/checkpoint/langgraph/store/base/__init__.py +++ b/libs/checkpoint/langgraph/store/base/__init__.py @@ -159,11 +159,15 @@ class InvalidNamespaceError(ValueError): def _validate_namespace(namespace: tuple[str, ...]) -> None: + if not namespace: + raise ValueError("Namespace cannot be empty.") for label in namespace: if "." in label: raise InvalidNamespaceError( f"Invalid namespace label '{label}'. Namespace labels cannot contain periods ('.')." ) + elif not label: + raise ValueError("Namespace labels cannot be empty strings.") class BaseStore(ABC): diff --git a/libs/checkpoint/tests/test_store.py b/libs/checkpoint/tests/test_store.py index 915b91e0a..03d3297f9 100644 --- a/libs/checkpoint/tests/test_store.py +++ b/libs/checkpoint/tests/test_store.py @@ -2,9 +2,10 @@ import asyncio from datetime import datetime from typing import Iterable +import pytest from pytest_mock import MockerFixture -from langgraph.store.base import GetOp, Item, Op, Result +from langgraph.store.base import GetOp, InvalidNamespaceError, Item, Op, Result from langgraph.store.base.batch import AsyncBatchedBaseStore from langgraph.store.memory import InMemoryStore @@ -259,3 +260,26 @@ def test_list_namespaces_empty_store() -> None: result = store.list_namespaces() assert result == [] + + +async def test_cannot_put_empty_namespace() -> None: + store = InMemoryStore() + doc = {"foo": "bar"} + + with pytest.raises(InvalidNamespaceError): + store.put([], "foo", doc) + + with pytest.raises(InvalidNamespaceError): + await store.aput([], "foo", doc) + + with pytest.raises(InvalidNamespaceError): + store.put(["the", "thing.about"], "foo", doc) + + with pytest.raises(InvalidNamespaceError): + await store.aput(["the", "thing.about"], "foo", doc) + + with pytest.raises(InvalidNamespaceError): + store.put(["some", "fun", ""], "foo", doc) + + with pytest.raises(InvalidNamespaceError): + await store.aput(["some", "fun", ""], "foo", doc)