LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
[FIX] Keep the Prompt Studio back-reference in tool registry listing; drop dead adapter-choices route (#2204)
Hari John Kuriakosec6c4609a01c2
diff --git a/backend/prompt_studio/prompt_studio_core_v2/urls.py b/backend/prompt_studio/prompt_studio_core_v2/urls.pyindex 290576b29..5cd58c1f8 100644--- a/backend/prompt_studio/prompt_studio_core_v2/urls.py+++ b/backend/prompt_studio/prompt_studio_core_v2/urls.py@@ -30,9 +30,6 @@ prompt_studio_profilemanager = PromptStudioCoreView.as_view(prompt_studio_prompt_index = PromptStudioCoreView.as_view({"post": "index_document"})prompt_studio_prompt_response = PromptStudioCoreView.as_view({"post": "fetch_response"})-prompt_studio_adapter_choices = PromptStudioCoreView.as_view(- {"get": "get_adapter_choices"}-)prompt_studio_bulk_fetch_response = PromptStudioCoreView.as_view({"post": "bulk_fetch_response"})@@ -125,11 +122,6 @@ urlpatterns = format_suffix_patterns(prompt_studio_bulk_fetch_response,name="prompt-studio-bulk-fetch-response",),- path(- "prompt-studio/adapter-choices/",- prompt_studio_adapter_choices,- name="prompt-studio-adapter-choices",- ),path("prompt-studio/single-pass-extraction/<uuid:pk>",prompt_studio_single_pass_extraction,diff --git a/backend/prompt_studio/prompt_studio_registry_v2/constants.py b/backend/prompt_studio/prompt_studio_registry_v2/constants.pyindex 35d665485..d1a7c8289 100644--- a/backend/prompt_studio/prompt_studio_registry_v2/constants.py+++ b/backend/prompt_studio/prompt_studio_registry_v2/constants.py@@ -88,6 +88,11 @@ class JsonSchemaKey:EMBEDDING_SUFFIX = "embedding_suffix"FUNCTION_NAME = "function_name"PROMPT_REGISTRY_ID = "prompt_registry_id"+ CUSTOM_TOOL = "custom_tool"+ # Deliberately not `tool_id`: in the workflow domain that consumes the same+ # listing, `tool_id` means the tool's *function name* (see+ # `ToolInstance.tool_id`), not a Prompt Studio project id.+ PROMPT_STUDIO_TOOL_ID = "prompt_studio_tool_id"NOTES = "NOTES"TOOL_SETTINGS = "tool_settings"ENABLE_CHALLENGE = "enable_challenge"diff --git a/backend/prompt_studio/prompt_studio_registry_v2/prompt_studio_registry_helper.py b/backend/prompt_studio/prompt_studio_registry_v2/prompt_studio_registry_helper.pyindex 8d88d2c00..fac9acd7d 100644--- a/backend/prompt_studio/prompt_studio_registry_v2/prompt_studio_registry_helper.py+++ b/backend/prompt_studio/prompt_studio_registry_v2/prompt_studio_registry_helper.py@@ -484,6 +484,26 @@ class PromptStudioRegistryHelper:tool_metadata[JsonSchemaKey.FUNCTION_NAME] = prompts.get(JsonSchemaKey.PROMPT_REGISTRY_ID)+ # Back-reference to the Prompt Studio project that produced this+ # entry. `function_name` is the registry UUID, so without this the+ # only correlator left is `name`, which is ambiguous when two+ # projects share one.+ #+ # Read from the `custom_tool` FK column and published as+ # `prompt_studio_tool_id`. Not `tool_id`: consumers of this listing+ # also POST to `tool_instance/`, where `tool_id` means the tool's+ # *function name* (`ToolInstance.tool_id`), so that key already has+ # an incompatible meaning in the same call path.+ #+ # Stringified so the value does not depend on DRF's renderer to+ # leave this dict, and so it compares like-for-like against+ # `function_name`, which DRF's UUIDField already renders as a str.+ # `fetch_tools_descriptions` emits no such key at all, so consumers+ # must treat a missing key and `None` alike.+ custom_tool_id = prompts.get(JsonSchemaKey.CUSTOM_TOOL)+ tool_metadata[JsonSchemaKey.PROMPT_STUDIO_TOOL_ID] = (+ str(custom_tool_id) if custom_tool_id is not None else None+ )tool_list.append(tool_metadata)tool_metadata = {}return tool_listdiff --git a/backend/prompt_studio/prompt_studio_registry_v2/tests/test_fetch_json_for_registry.py b/backend/prompt_studio/prompt_studio_registry_v2/tests/test_fetch_json_for_registry.pynew file mode 100644index 000000000..dd26428d3--- /dev/null+++ b/backend/prompt_studio/prompt_studio_registry_v2/tests/test_fetch_json_for_registry.py@@ -0,0 +1,265 @@+"""Regression tests for ``PromptStudioRegistryHelper.fetch_json_for_registry``.++The registry listing served at ``tool/`` must carry ``prompt_studio_tool_id``+-- the Prompt Studio project that produced each entry. ``function_name`` cannot+serve that role: it is the ``prompt_registry_id``, a UUID minted per registry+row and unrelated to the project's id. Without the back-reference the only+correlator left is ``name``, which is ambiguous whenever two projects share one.++The key is deliberately not ``tool_id``: consumers of this listing also POST to+``tool_instance/``, where ``tool_id`` means the tool's *function name*+(``ToolInstance.tool_id``), so that name already has an incompatible meaning in+the same call path.++The two serialized fields this suite handles have different DRF field classes,+and therefore different Python types on the way out -- the fixtures model each+as the real serializer emits it:++* ``prompt_registry_id`` is a ``models.UUIDField`` -> DRF ``UUIDField``, whose+ ``to_representation`` returns a ``str``.+* ``custom_tool`` is a ``OneToOneField`` -> DRF ``PrimaryKeyRelatedField``,+ whose ``to_representation`` returns the raw pk -- a ``uuid.UUID``.++Two properties are pinned deliberately, because neither lives in the projection+loop:++* ``PromptStudioRegistrySerializer`` must actually emit ``custom_tool``. It does+ so only because ``Meta.fields`` is ``"__all__"``. Narrowing that to an+ explicit list -- a plausible optimization, since ``"__all__"`` also ships the+ large ``tool_property``/``tool_spec``/``tool_metadata`` blobs on every listing+ -- would empty the back-reference for every row without touching this+ projection at all. ``test_serializer_emits_the_custom_tool_column`` asserts+ against rendered output rather than the declared field set, so a+ ``to_representation`` override that drops the key is caught too.+* ``function_name`` must remain the registry UUID. "Fixing" a caller by+ redefining it would break tool resolution.++Collaborators are patched on the helper module per-test, so no database is+touched. The module is imported for real, which means this file needs Django+configured -- it runs in the rig's ``unit-backend`` tier, where+``DJANGO_SETTINGS_MODULE`` is set (``tests/groups.yaml``). Under a bare+``pytest`` with no settings module it fails at collection, as every+Django-coupled suite here does.+"""++from __future__ import annotations++import uuid+from types import SimpleNamespace+from typing import Any+from unittest.mock import MagicMock, patch++from prompt_studio.prompt_studio_registry_v2 import (+ prompt_studio_registry_helper as _psr_mod,+)+from prompt_studio.prompt_studio_registry_v2.serializers import (+ PromptStudioRegistrySerializer,+)++PromptStudioRegistryHelper = _psr_mod.PromptStudioRegistryHelper++# Typed as the real serializer emits each field -- see the module docstring.+PROMPT_REGISTRY_ID = str(uuid.UUID("99999999-8888-7777-6666-555555555555"))+CUSTOM_TOOL_ID = uuid.UUID("11111111-2222-3333-4444-555555555555")+++def _row(**overrides: Any) -> dict[str, Any]:+ """A registry row shaped as ``PromptStudioRegistrySerializer`` emits it."""+ row = {+ "name": "Invoice extractor",+ "description": "Extracts invoice fields",+ "icon": "icon-data",+ "prompt_registry_id": PROMPT_REGISTRY_ID,+ "custom_tool": CUSTOM_TOOL_ID,+ }+ row.update(overrides)+ return row+++def _fetch(rows: list[dict[str, Any]]) -> list[dict[str, Any]]:+ """Run the real projection over ``rows``, stubbing the ORM and serializer.++ Patches land on the helper module, so the function under test is the real+ one -- a rename or restructure surfaces as an import error or a failing+ assertion here, never as a silent skip. The serializer's call shape is+ asserted too: dropping ``many=True`` would make DRF treat the queryset as a+ single instance and break the listing in production.+ """+ user = MagicMock(name="user")+ serializer = MagicMock(name="PromptStudioRegistrySerializer")+ serializer.return_value.data = rows+ with (+ patch.object(_psr_mod, "PromptStudioRegistry") as registry,+ patch.object(_psr_mod, "PromptStudioRegistrySerializer", serializer),+ ):+ queryset = MagicMock(name="queryset")+ registry.objects.list_tools.return_value = queryset++ result = PromptStudioRegistryHelper.fetch_json_for_registry(user=user)++ registry.objects.list_tools.assert_called_once_with(user)+ serializer.assert_called_once_with(instance=queryset, many=True)+ return result+++def test_serializer_emits_the_custom_tool_column() -> None:+ """The projection can only publish what the serializer serializes.++ Renders the way production does -- ``many=True``, through ``ListSerializer``+ -- so every way the key can vanish is caught: narrowing ``Meta.fields``,+ ``exclude``, ``write_only``, a field- or serializer-level+ ``to_representation`` override, and a ``Meta.list_serializer_class`` that+ strips the key on the list path only. Rendering a single instance would miss+ that last one, since production never serializes one row at a time.++ The stub stands in for a saved row so this stays DB-free; a real instance+ would make ``PrimaryKeyRelatedField`` evaluate its queryset. It only needs+ the attributes asserted on below: DRF raises ``SkipField`` for anything+ missing on the stub and omits that key, so an unrelated field added to the+ model later is quietly skipped rather than breaking this test.+ """+ stub = SimpleNamespace(+ prompt_registry_id=uuid.UUID(PROMPT_REGISTRY_ID),+ custom_tool=SimpleNamespace(pk=CUSTOM_TOOL_ID),+ name="Invoice extractor",+ description="Extracts invoice fields",+ icon="icon-data",+ url="",+ tool_property={},+ tool_spec={},+ tool_metadata={},+ shared_to_org=False,+ shared_users=SimpleNamespace(all=list),+ organization=None,+ created_by=None,+ modified_by=None,+ created_at=None,+ modified_at=None,+ )++ (rendered,) = PromptStudioRegistrySerializer([stub], many=True).data++ assert "custom_tool" in rendered, (+ "PromptStudioRegistrySerializer must serialize `custom_tool`; without "+ "it fetch_json_for_registry publishes prompt_studio_tool_id=None for "+ "every entry"+ )+ assert (+ rendered["custom_tool"] == CUSTOM_TOOL_ID+ ), "custom_tool must render as the raw pk; the projection stringifies it"+ assert isinstance(rendered["prompt_registry_id"], str), (+ "prompt_registry_id renders as a str, which is why the fixtures in "+ "this file model it as one"+ )+++def test_listing_exposes_the_prompt_studio_tool_id() -> None:+ """The fix: callers can correlate an entry with the project that made it."""+ (entry,) = _fetch([_row()])++ assert entry["prompt_studio_tool_id"] == str(CUSTOM_TOOL_ID), (+ "The registry listing must carry the Prompt Studio project id, "+ "otherwise callers can only match on the ambiguous `name`"+ )+++def test_function_name_remains_the_registry_id() -> None:+ """``function_name`` is the registry UUID, NOT the Prompt Studio project id.++ Pins the distinction that makes the back-reference necessary, and guards+ against someone "fixing" a caller by redefining ``function_name`` instead --+ which would break tool resolution. Both sides are compared as strings so the+ inequality cannot pass on a type mismatch alone.+ """+ (entry,) = _fetch([_row()])++ assert entry["function_name"] == PROMPT_REGISTRY_ID+ assert entry["function_name"] != entry["prompt_studio_tool_id"]+++def test_the_two_ids_are_compared_like_for_like() -> None:+ """A same-identifier collision must be detectable, not hidden by types.++ If both keys ever carried the same id, the guard in+ ``test_function_name_remains_the_registry_id`` has to fail. That only holds+ when the two values share a Python type; a ``UUID``-vs-``str`` mismatch+ would make the guard pass no matter what the values were.+ """+ shared = uuid.UUID("11111111-2222-3333-4444-555555555555")+ (entry,) = _fetch([_row(prompt_registry_id=str(shared), custom_tool=shared)])++ assert entry["function_name"] == entry["prompt_studio_tool_id"], (+ "Both keys must be rendered as the same type, so that comparing them "+ "is meaningful rather than trivially true"+ )+++def test_existing_keys_are_preserved() -> None:+ """The change is additive -- current consumers must not break."""+ (entry,) = _fetch([_row()])++ assert entry["name"] == "Invoice extractor"+ assert entry["description"] == "Extracts invoice fields"+ assert entry["icon"] == "icon-data"+++def test_legacy_row_without_a_linked_project() -> None:+ """``custom_tool`` is nullable, so unlinked legacy rows report None.++ They must not raise, and must not be dropped from the listing.+ """+ (entry,) = _fetch([_row(custom_tool=None)])++ assert entry["prompt_studio_tool_id"] is None+ assert entry["name"] == "Invoice extractor"+++def test_rows_do_not_bleed_into_each_other() -> None:+ """The loop reuses a dict and resets it per row.++ A missing reset would smear one project's back-reference onto the next,+ which is worse than the original bug: callers would correlate confidently+ and wrongly.+ """+ other_registry_id = str(uuid.UUID("aaaaaaaa-1111-2222-3333-444444444444"))+ other_custom_tool = uuid.UUID("bbbbbbbb-1111-2222-3333-444444444444")++ first, second = _fetch(+ [+ _row(),+ _row(+ name="Receipt extractor",+ description="Extracts receipt fields",+ icon="other-icon",+ prompt_registry_id=other_registry_id,+ custom_tool=other_custom_tool,+ ),+ ]+ )++ assert first["prompt_studio_tool_id"] == str(CUSTOM_TOOL_ID)+ assert second["prompt_studio_tool_id"] == str(other_custom_tool)+ assert first["function_name"] == PROMPT_REGISTRY_ID+ assert second["function_name"] == other_registry_id+++def test_two_projects_sharing_a_name_stay_distinguishable() -> None:+ """The exact scenario the old projection could not represent."""+ duplicate_name = "Invoice extractor"++ first, second = _fetch(+ [+ _row(name=duplicate_name),+ _row(+ name=duplicate_name,+ prompt_registry_id=str(uuid.UUID("cccccccc-1111-2222-3333-444444444444")),+ custom_tool=uuid.UUID("dddddddd-1111-2222-3333-444444444444"),+ ),+ ]+ )++ assert first["name"] == second["name"]+ assert first["prompt_studio_tool_id"] != second["prompt_studio_tool_id"], (+ "Two projects sharing a name must remain distinguishable by their "+ "back-reference -- this is the whole point of the fix"+ )