|
2 | 2 | # SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | 4 | import logging |
| 5 | +from typing import Any |
5 | 6 |
|
6 | 7 | import data_designer.config as dd |
7 | 8 | from data_designer.config.seed_source import SeedSource |
8 | 9 | from data_designer_nemo.errors import NDDInternalError, NDDInvalidConfigError |
9 | 10 | from data_designer_nemo.fileset_file_seed_source import FilesetFileSeedSource |
| 11 | +from data_designer_nemo.fileset_filesystem_provider import is_local_directory |
10 | 12 | from data_designer_nemo.secret_resolver import validate_secret |
11 | 13 | from nemo_platform import AsyncNeMoPlatform, NotFoundError, PermissionDeniedError |
12 | 14 | from nemo_platform.filesets import FilesetPathError, build_fileset_ref, parse_fileset_ref |
13 | 15 |
|
14 | 16 | logger = logging.getLogger(__name__) |
15 | 17 |
|
16 | | - |
17 | | -def get_seed_source(dd_config: dd.DataDesignerConfig) -> SeedSource | None: |
18 | | - return dd_config.seed_config.source if dd_config.seed_config else None |
| 18 | +_SUPPORTED_SEED_TYPES = {"directory", "file_contents", "hf", "nmp"} |
| 19 | +_UNSUPPORTED_SEED_TYPES_MESSAGE = ( |
| 20 | + "The NeMo Platform Data Designer service only supports seed data from HuggingFace " |
| 21 | + "or the NeMo Platform Files service (FilesetFile, Directory, or FileContents seed sources " |
| 22 | + "referencing fileset paths). Upload your data to the Files service, adjust your config, and try again." |
| 23 | +) |
| 24 | +_DATAFRAME_SEED_TYPE = "df" |
| 25 | +_DATAFRAME_SEED_TYPE_MESSAGE = ( |
| 26 | + "Dataframe seed sources (seed_type=df) are not supported on the NeMo Platform. TODO: more detail here!" |
| 27 | +) |
19 | 28 |
|
20 | 29 |
|
21 | | -async def validate_seed(dd_config: dd.DataDesignerConfig, workspace: str, sdk: AsyncNeMoPlatform) -> str | None: |
22 | | - if (seed_source := get_seed_source(dd_config)) is None: |
| 30 | +async def validate_seed( |
| 31 | + dd_config: dd.DataDesignerConfig, |
| 32 | + workspace: str, |
| 33 | + sdk: AsyncNeMoPlatform, |
| 34 | + is_local: bool, |
| 35 | +) -> str | None: |
| 36 | + if (seed_source := _get_seed_source(dd_config)) is None: |
23 | 37 | return None |
24 | 38 |
|
25 | | - if isinstance(seed_source, dd.HuggingFaceSeedSource) and (token := seed_source.token) is not None: |
26 | | - await validate_secret(sdk, token, workspace) |
| 39 | + _validate_seed_type_for_execution_context( |
| 40 | + seed_source.seed_type, |
| 41 | + is_local=is_local, |
| 42 | + ) |
| 43 | + |
| 44 | + if isinstance(seed_source, dd.HuggingFaceSeedSource): |
| 45 | + # In local execution context, a HF seed source token will always "resolve" |
| 46 | + # because the composite secret resolver includes a plaintext resolver. |
| 47 | + # In remote execution context, a HF seed source token must be a reference |
| 48 | + # to a Nemo Platform secret (if provided). |
| 49 | + if not is_local and (token := seed_source.token) is not None: |
| 50 | + await validate_secret(sdk, token, workspace) |
27 | 51 | return None |
28 | 52 |
|
| 53 | + if is_local and isinstance(seed_source, dd.DirectorySeedSource | dd.FileContentsSeedSource): |
| 54 | + if is_local_directory(seed_source.path): |
| 55 | + return None |
| 56 | + |
29 | 57 | if isinstance(seed_source, FilesetFileSeedSource | dd.DirectorySeedSource | dd.FileContentsSeedSource): |
30 | 58 | return await _validate_seed_from_files_service(seed_source, workspace, sdk) |
31 | 59 |
|
@@ -78,3 +106,63 @@ async def _validate_seed_from_files_service( |
78 | 106 | raise NDDInvalidConfigError(f"Path {fragment!r} not found in fileset {fully_qualified_fileset_name!r}") |
79 | 107 |
|
80 | 108 | return canonical_root |
| 109 | + |
| 110 | + |
| 111 | +def validate_seed_source_for_execution_context(data: Any, *, is_local: bool) -> None: |
| 112 | + """Raises if a raw request seed source is unsupported for the execution context. |
| 113 | +
|
| 114 | + This function is used in Pydantic validators defined on the preview and job request models, |
| 115 | + both of which carry a `config: dd.DataDesignerConfig` field. |
| 116 | +
|
| 117 | + This function is used in "before"-style Pydantic validators, where the data argument is typed |
| 118 | + as Any. We run in the before context to preempt less-useful error messages from the DD library: |
| 119 | + - missing dataframe field (we don't serialize dataframes over the wire) |
| 120 | + - file does not exist (the client's local fs != the service's local fs) |
| 121 | +
|
| 122 | + The validators using this function only care about preventing unsupported seed types. All the |
| 123 | + other standard Pydantic validation will get applied by FastAPI parsing the request; this does |
| 124 | + not bypass that. So, we can safely ignore all Exceptions (most commonly KeyError, on requests |
| 125 | + that don't include a seed_config at all) and index our way straight to the deeply nested field |
| 126 | + we care about for this particular validation. |
| 127 | +
|
| 128 | + Per the Pydantic v2 contract, "before"-mode validators may raise ``ValueError``, |
| 129 | + ``AssertionError``, or ``PydanticCustomError`` — anything else (including our |
| 130 | + ``NDDInvalidConfigError``) propagates raw out of ``model_validate`` and is not wrapped in |
| 131 | + ``pydantic.ValidationError``. That breaks ``except ValidationError`` clauses in CLI / framework |
| 132 | + code that turn validation problems into clean user-facing messages. To keep those code paths |
| 133 | + working *and* keep ``NDDInvalidConfigError`` as the canonical error class for non-Pydantic |
| 134 | + callers, we translate at this boundary: catch the plugin's error class and re-raise as a |
| 135 | + ``ValueError`` carrying the same message. |
| 136 | + """ |
| 137 | + seed_type = _get_raw_seed_type(data) |
| 138 | + if seed_type is None: |
| 139 | + return |
| 140 | + |
| 141 | + try: |
| 142 | + _validate_seed_type_for_execution_context(seed_type, is_local=is_local) |
| 143 | + except NDDInvalidConfigError as exc: |
| 144 | + raise ValueError(str(exc)) from exc |
| 145 | + |
| 146 | + |
| 147 | +def _validate_seed_type_for_execution_context(seed_type: str, *, is_local: bool) -> None: |
| 148 | + """Raises if a seed source type is unsupported in this execution context.""" |
| 149 | + if is_local: |
| 150 | + if seed_type == _DATAFRAME_SEED_TYPE: |
| 151 | + raise NDDInvalidConfigError(_DATAFRAME_SEED_TYPE_MESSAGE) |
| 152 | + return |
| 153 | + |
| 154 | + if seed_type not in _SUPPORTED_SEED_TYPES: |
| 155 | + raise NDDInvalidConfigError(_UNSUPPORTED_SEED_TYPES_MESSAGE) |
| 156 | + |
| 157 | + |
| 158 | +def _get_seed_source(dd_config: dd.DataDesignerConfig) -> SeedSource | None: |
| 159 | + return dd_config.seed_config.source if dd_config.seed_config else None |
| 160 | + |
| 161 | + |
| 162 | +def _get_raw_seed_type(data: Any) -> str | None: |
| 163 | + try: |
| 164 | + seed_type = data["config"]["seed_config"]["source"]["seed_type"] |
| 165 | + except Exception: |
| 166 | + return None |
| 167 | + |
| 168 | + return seed_type if isinstance(seed_type, str) else None |
0 commit comments