feat: Add url field to PaginatedResponse and populate it using route path

Resolves: https://github.com/meta-llama/llama-stack/issues/1847
Changes:
- responses.py: added optional `url` field to PaginatedResponse
- server.py: automatically populate the URL field with route path
This commit is contained in:
Rohan Awhad 2025-06-06 22:24:30 -04:00
parent 33ecefd284
commit e28a57ad76
4 changed files with 14 additions and 1 deletions

View file

@ -10711,6 +10711,10 @@
"has_more": { "has_more": {
"type": "boolean", "type": "boolean",
"description": "Whether there are more items available after this set" "description": "Whether there are more items available after this set"
},
"url": {
"type": "string",
"description": "The URL for accessing this list"
} }
}, },
"additionalProperties": false, "additionalProperties": false,

View file

@ -7511,6 +7511,9 @@ components:
type: boolean type: boolean
description: >- description: >-
Whether there are more items available after this set Whether there are more items available after this set
url:
type: string
description: The URL for accessing this list
additionalProperties: false additionalProperties: false
required: required:
- data - data

View file

@ -23,7 +23,9 @@ class PaginatedResponse(BaseModel):
:param data: The list of items for the current page :param data: The list of items for the current page
:param has_more: Whether there are more items available after this set :param has_more: Whether there are more items available after this set
:param url: The URL for accessing this list
""" """
data: list[dict[str, Any]] data: list[dict[str, Any]]
has_more: bool has_more: bool
url: str | None = None

View file

@ -30,6 +30,7 @@ from fastapi.responses import JSONResponse, StreamingResponse
from openai import BadRequestError from openai import BadRequestError
from pydantic import BaseModel, ValidationError from pydantic import BaseModel, ValidationError
from llama_stack.apis.common.responses import PaginatedResponse
from llama_stack.distribution.datatypes import AuthenticationRequiredError, LoggingConfig, StackRunConfig from llama_stack.distribution.datatypes import AuthenticationRequiredError, LoggingConfig, StackRunConfig
from llama_stack.distribution.distribution import builtin_automatically_routed_apis from llama_stack.distribution.distribution import builtin_automatically_routed_apis
from llama_stack.distribution.request_headers import PROVIDER_DATA_VAR, User, request_provider_data_context from llama_stack.distribution.request_headers import PROVIDER_DATA_VAR, User, request_provider_data_context
@ -230,7 +231,10 @@ def create_dynamic_typed_route(func: Any, method: str, route: str) -> Callable:
return StreamingResponse(gen, media_type="text/event-stream") return StreamingResponse(gen, media_type="text/event-stream")
else: else:
value = func(**kwargs) value = func(**kwargs)
return await maybe_await(value) result = await maybe_await(value)
if isinstance(result, PaginatedResponse) and result.url is None:
result.url = route
return result
except Exception as e: except Exception as e:
logger.exception(f"Error executing endpoint {route=} {method=}") logger.exception(f"Error executing endpoint {route=} {method=}")
raise translate_exception(e) from e raise translate_exception(e) from e