From 7e80a6ecd123621e8dfee01b88aa7a3c099268bf Mon Sep 17 00:00:00 2001 From: Poseidon Date: Thu, 18 Jun 2026 08:51:49 +0000 Subject: [PATCH] fix: resolve benchmark prompt assets without asserting concrete asset types RapidataBenchmark.__instantiate_prompts asserted that each prompt's prompt_asset.actual_instance was a FileAssetModel and then indexed metadata["sourceUrl"]. Neither holds for the current API response: the prompts endpoint returns an IAssetModel oneOf wrapper whose actual_instance is an IAssetModel* variant (file/multi/text/null), and only URL-registered assets carry a "sourceUrl" metadata entry. File-uploaded prompts only have originalFilename/imageDimension, so the assert raised AssertionError (and the index would otherwise KeyError) on any benchmark with file assets. Because identifiers/prompts/prompt_assets/tags all lazily call this method, and add_model/add_prompts read self.identifiers, the crash blocked the whole participant-adding flow. Resolve the source URL defensively (mirroring RapidataFlowItem._extract_asset_key): navigate actual_instance -> metadata -> "sourceUrl" with getattr/.get and fall back to None when no URL is present, rather than asserting concrete types. Co-Authored-By: Claude Opus 4.8 Co-Authored-By: karl --- .../benchmark/rapidata_benchmark.py | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/src/rapidata/rapidata_client/benchmark/rapidata_benchmark.py b/src/rapidata/rapidata_client/benchmark/rapidata_benchmark.py index f82bdd691..749bb7d98 100644 --- a/src/rapidata/rapidata_client/benchmark/rapidata_benchmark.py +++ b/src/rapidata/rapidata_client/benchmark/rapidata_benchmark.py @@ -12,6 +12,7 @@ if TYPE_CHECKING: import pandas as pd + from rapidata.api_client.models.i_asset_model import IAssetModel from rapidata.rapidata_client.audience._audience_base import RapidataAudienceBase from rapidata.rapidata_client.benchmark.leaderboard.rapidata_leaderboard import ( RapidataLeaderboard, @@ -51,12 +52,27 @@ def __init__(self, name: str, id: str, openapi_service: OpenAPIService): ) self._prompt_uploader = BenchmarkPromptUploader(id, openapi_service) + @staticmethod + def __source_url(asset: "IAssetModel | None") -> str | None: + """Best-effort source URL for a prompt asset. + + The prompts endpoint returns an ``IAssetModel`` oneOf wrapper whose + ``actual_instance`` is a file/multi/text/null variant; only assets + registered from a URL carry a ``sourceUrl`` metadata entry. Navigate + defensively and fall back to None rather than asserting a concrete type + (the variants are ``IAssetModel*`` / ``IMetadataModel*``, not the bare + ``FileAssetModel`` / ``SourceUrlMetadataModel`` the API no longer returns). + """ + instance = getattr(asset, "actual_instance", None) + metadata = getattr(instance, "metadata", None) + if not metadata: + return None + source_url = metadata.get("sourceUrl") + actual = getattr(source_url, "actual_instance", None) + return getattr(actual, "url", None) + def __instantiate_prompts(self) -> None: from rapidata.rapidata_client.config import tracer - from rapidata.api_client.models.file_asset_model import FileAssetModel - from rapidata.api_client.models.source_url_metadata_model import ( - SourceUrlMetadataModel, - ) with tracer.start_as_current_span("RapidataBenchmark.__instantiate_prompts"): self.__prompts = [] @@ -86,18 +102,7 @@ def __instantiate_prompts(self) -> None: self.__prompts.append(prompt.original_prompt) self.__english_prompts.append(prompt.english_prompt) self.__identifiers.append(prompt.identifier) - if prompt.prompt_asset is None: - self.__prompt_assets.append(None) - else: - assert isinstance( - prompt.prompt_asset.actual_instance, FileAssetModel - ) - source_url = prompt.prompt_asset.actual_instance.metadata[ - "sourceUrl" - ].actual_instance - assert isinstance(source_url, SourceUrlMetadataModel) - self.__prompt_assets.append(source_url.url) - + self.__prompt_assets.append(self.__source_url(prompt.prompt_asset)) self.__tags.append(prompt.tags) if current_page >= total_pages: break