From 272a3e9937640dd369fc542d4a095573b1a8e619 Mon Sep 17 00:00:00 2001 From: Nathan Weinberg <31703736+nathan-weinberg@users.noreply.github.com> Date: Wed, 30 Jul 2025 17:52:46 -0400 Subject: [PATCH] chore: standardize dataset not found error (#2962) # What does this PR do? 1. Adds a broad schema for custom exception classes in the Llama Stack project 2. Creates a new `DatasetNotFoundError` class 3. Implements the new class where appropriate Relates to #2379 Signed-off-by: Nathan Weinberg --- llama_stack/apis/common/errors.py | 13 +++++++++++++ llama_stack/distribution/routing_tables/datasets.py | 5 +++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/llama_stack/apis/common/errors.py b/llama_stack/apis/common/errors.py index fb52dc772..5ad2a34f0 100644 --- a/llama_stack/apis/common/errors.py +++ b/llama_stack/apis/common/errors.py @@ -4,6 +4,11 @@ # This source code is licensed under the terms described in the LICENSE file in # the root directory of this source tree. +# Custom Llama Stack Exception classes should follow the following schema +# 1. All classes should inherit from an existing Built-In Exception class: https://docs.python.org/3/library/exceptions.html +# 2. All classes should have a custom error message with the goal of informing the Llama Stack user specifically +# 3. All classes should propogate the inherited __init__ function otherwise via 'super().__init__(message)' + class UnsupportedModelError(ValueError): """raised when model is not present in the list of supported models""" @@ -19,3 +24,11 @@ class ModelNotFoundError(ValueError): def __init__(self, model_name: str) -> None: message = f"Model '{model_name}' not found. Use client.models.list() to list available models." super().__init__(message) + + +class DatasetNotFoundError(ValueError): + """raised when Llama Stack cannot find a referenced dataset""" + + def __init__(self, dataset_name: str) -> None: + message = f"Dataset '{dataset_name}' not found. Use client.datasets.list() to list available datasets." + super().__init__(message) diff --git a/llama_stack/distribution/routing_tables/datasets.py b/llama_stack/distribution/routing_tables/datasets.py index 47894313a..508c542a2 100644 --- a/llama_stack/distribution/routing_tables/datasets.py +++ b/llama_stack/distribution/routing_tables/datasets.py @@ -7,6 +7,7 @@ import uuid from typing import Any +from llama_stack.apis.common.errors import DatasetNotFoundError from llama_stack.apis.datasets import ( Dataset, DatasetPurpose, @@ -35,7 +36,7 @@ class DatasetsRoutingTable(CommonRoutingTableImpl, Datasets): async def get_dataset(self, dataset_id: str) -> Dataset: dataset = await self.get_object_by_identifier("dataset", dataset_id) if dataset is None: - raise ValueError(f"Dataset '{dataset_id}' not found") + raise DatasetNotFoundError(dataset_id) return dataset async def register_dataset( @@ -88,5 +89,5 @@ class DatasetsRoutingTable(CommonRoutingTableImpl, Datasets): async def unregister_dataset(self, dataset_id: str) -> None: dataset = await self.get_dataset(dataset_id) if dataset is None: - raise ValueError(f"Dataset {dataset_id} not found") + raise DatasetNotFoundError(dataset_id) await self.unregister_object(dataset)