Skip to content

Commit 58ff79e

Browse files
authored
Merge pull request lightspeed-core#912 from tisnik/lcore-731-updated-docstrings-in-utils-module
LCORE-731: Updated docstrings in utils module
2 parents 982efa9 + d0e0c32 commit 58ff79e

11 files changed

Lines changed: 204 additions & 32 deletions

src/utils/common.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ async def _register_mcp_toolgroups_async(
6969
This function performs network calls against the provided async client and does not
7070
catch exceptions raised by those calls — any exceptions from the client (e.g., RPC
7171
or HTTP errors) will propagate to the caller.
72+
73+
Parameters:
74+
client (AsyncLlamaStackClient): The LlamaStack async client used to
75+
query and register toolgroups.
76+
mcp_servers (List[ModelContextProtocolServer]): MCP server descriptors
77+
to ensure are registered.
78+
logger (Logger): Logger used for debug messages about registration
79+
progress.
7280
"""
7381
# Get registered tools
7482
registered_toolgroups = await client.toolgroups.list()
@@ -101,6 +109,10 @@ def run_once_async(func: Callable) -> Callable:
101109
Later invocations return/await the same Task, receiving the same result or
102110
propagated exception. Requires an active running event loop when the
103111
wrapped function is first called.
112+
113+
Returns:
114+
Any: The result produced by the wrapped coroutine, or the exception it
115+
raised propagated to callers.
104116
"""
105117
task = None
106118

@@ -114,6 +126,9 @@ async def wrapper(*args: Any, **kwargs: Any) -> Any:
114126
Subsequent calls return the same awaited task result. Exceptions raised
115127
by the task propagate to callers. Requires an active running event loop
116128
when first called.
129+
130+
Returns:
131+
The awaited result of the wrapped coroutine.
117132
"""
118133
nonlocal task
119134
if task is None:

src/utils/endpoints.py

Lines changed: 105 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,17 @@ def get_system_prompt(query_request: QueryRequest, config: AppConfig) -> str:
205205

206206

207207
def get_topic_summary_system_prompt(config: AppConfig) -> str:
208-
"""Get the topic summary system prompt."""
208+
"""
209+
Get the topic summary system prompt.
210+
211+
Parameters:
212+
config (AppConfig): Application configuration from which to read
213+
customization/profile settings.
214+
215+
Returns:
216+
str: The topic summary system prompt from the active custom profile if
217+
set, otherwise the default prompt.
218+
"""
209219
# profile takes precedence for setting prompt
210220
if (
211221
config.customization is not None
@@ -223,8 +233,9 @@ def validate_model_provider_override(
223233
) -> None:
224234
"""Validate whether model/provider overrides are allowed by RBAC.
225235
226-
Raises HTTP 403 if the request includes model or provider and the caller
227-
lacks Action.MODEL_OVERRIDE permission.
236+
Raises:
237+
HTTPException: HTTP 403 if the request includes model or provider and
238+
the caller lacks Action.MODEL_OVERRIDE permission.
228239
"""
229240
if (query_request.model is not None or query_request.provider is not None) and (
230241
Action.MODEL_OVERRIDE not in authorized_actions
@@ -242,7 +253,24 @@ def store_conversation_into_cache(
242253
_skip_userid_check: bool,
243254
topic_summary: str | None,
244255
) -> None:
245-
"""Store one part of conversation into conversation history cache."""
256+
"""
257+
Store one part of conversation into conversation history cache.
258+
259+
If a conversation cache type is configured but the cache instance is not
260+
initialized, the function logs a warning and returns without persisting
261+
anything.
262+
263+
Parameters:
264+
config (AppConfig): Application configuration that may contain
265+
conversation cache settings and instance.
266+
user_id (str): Owner identifier used as the cache key.
267+
conversation_id (str): Conversation identifier used as the cache key.
268+
cache_entry (CacheEntry): Entry to insert or append to the conversation history.
269+
_skip_userid_check (bool): When true, bypasses enforcing that the cache
270+
operation must match the user id.
271+
topic_summary (str | None): Optional topic summary to store alongside
272+
the conversation; ignored if None or empty.
273+
"""
246274
if config.conversation_cache_configuration.type is not None:
247275
cache = config.conversation_cache
248276
if cache is None:
@@ -366,10 +394,12 @@ async def get_temp_agent(
366394
This function creates a new agent without persistence, shields, or tools.
367395
Useful for temporary operations or one-off queries, such as validating a
368396
question or generating a summary.
369-
Args:
397+
398+
Parameters:
370399
client: The AsyncLlamaStackClient to use for the request.
371400
model_id: The ID of the model to use.
372401
system_prompt: The system prompt/instructions for the agent.
402+
373403
Returns:
374404
tuple[AsyncAgent, str]: A tuple containing the agent and session_id.
375405
"""
@@ -412,7 +442,23 @@ def create_rag_chunks_dict(summary: TurnSummary) -> list[dict[str, Any]]:
412442
def _process_http_source(
413443
src: str, doc_urls: set[str]
414444
) -> tuple[AnyUrl | None, str] | None:
415-
"""Process HTTP source and return (doc_url, doc_title) tuple."""
445+
"""
446+
Process HTTP source and return (doc_url, doc_title) tuple.
447+
448+
Parameters:
449+
src (str): The source URL string to process.
450+
doc_urls (set[str]): Set of already-seen source strings; the function
451+
will add `src` to this set when it is new.
452+
453+
Returns:
454+
tuple[AnyUrl | None, str] | None: A tuple (validated_url, doc_title)
455+
when `src` was not previously seen:
456+
- `validated_url`: an `AnyUrl` instance if `src` is a valid URL, or
457+
`None` if validation failed.
458+
- `doc_title`: the last path segment of the URL or `src` if no path
459+
segment is present.
460+
Returns `None` if `src` was already present in `doc_urls`.
461+
"""
416462
if src not in doc_urls:
417463
doc_urls.add(src)
418464
try:
@@ -433,7 +479,29 @@ def _process_document_id(
433479
metas_by_id: dict[str, dict[str, Any]],
434480
metadata_map: dict[str, Any] | None,
435481
) -> tuple[AnyUrl | None, str] | None:
436-
"""Process document ID and return (doc_url, doc_title) tuple."""
482+
"""
483+
Process document ID and return (doc_url, doc_title) tuple.
484+
485+
Parameters:
486+
src (str): Document identifier to process.
487+
doc_ids (set[str]): Set of already-seen document IDs; the function adds `src` to this set.
488+
doc_urls (set[str]): Set of already-seen document URLs; the function
489+
adds discovered URLs to this set to avoid duplicates.
490+
metas_by_id (dict[str, dict[str, Any]]): Mapping of document IDs to
491+
metadata dicts that may
492+
contain `docs_url` and
493+
`title`.
494+
metadata_map (dict[str, Any] | None): If provided (truthy), indicates
495+
metadata is available and enables
496+
metadata lookup; when falsy,
497+
metadata lookup is skipped.
498+
499+
Returns:
500+
tuple[AnyUrl | None, str] | None: `(validated_url, doc_title)` where
501+
`validated_url` is a validated `AnyUrl` or `None` and `doc_title` is
502+
the chosen title string; returns `None` if the `src` or its URL was
503+
already processed.
504+
"""
437505
if src in doc_ids:
438506
return None
439507
doc_ids.add(src)
@@ -500,6 +568,17 @@ def _process_rag_chunks_for_documents(
500568
Process RAG chunks and return a list of (doc_url, doc_title) tuples.
501569
502570
This is the core logic shared between both return formats.
571+
572+
Parameters:
573+
rag_chunks (list): Iterable of RAG chunk objects; each chunk must
574+
provide a `source` attribute (e.g., an HTTP URL or a document ID).
575+
metadata_map (dict[str, Any] | None): Optional mapping of document IDs
576+
to metadata dictionaries used to resolve titles and document URLs.
577+
578+
Returns:
579+
list[tuple[AnyUrl | None, str]]: Ordered list of tuples where the first
580+
element is a validated URL object or `None` (if no URL is available)
581+
and the second element is the document title.
503582
"""
504583
doc_urls: set[str] = set()
505584
doc_ids: set[str] = set()
@@ -547,7 +626,7 @@ def create_referenced_documents(
547626
optional metadata enrichment, deduplication, and proper URL handling. It can return
548627
either ReferencedDocument objects (for query endpoint) or dictionaries (for streaming).
549628
550-
Args:
629+
Parameters:
551630
rag_chunks: List of RAG chunks with source information
552631
metadata_map: Optional mapping containing metadata about referenced documents
553632
return_dict_format: If True, returns list of dicts; if False, returns list of
@@ -580,6 +659,16 @@ def create_referenced_documents_with_metadata(
580659
Create referenced documents from RAG chunks with metadata enrichment for streaming.
581660
582661
This function now returns ReferencedDocument objects for consistency with the query endpoint.
662+
663+
Parameters:
664+
summary (TurnSummary): Summary object containing `rag_chunks` to be processed.
665+
metadata_map (dict[str, Any]): Metadata keyed by document id used to
666+
derive or enrich document `doc_url` and `doc_title`.
667+
668+
Returns:
669+
list[ReferencedDocument]: ReferencedDocument objects with `doc_url` and
670+
`doc_title` populated; `doc_url` may be `None` if no valid URL could be
671+
determined.
583672
"""
584673
document_entries = _process_rag_chunks_for_documents(
585674
summary.rag_chunks, metadata_map
@@ -598,6 +687,14 @@ def create_referenced_documents_from_chunks(
598687
599688
This is a backward compatibility wrapper around the unified
600689
create_referenced_documents function.
690+
691+
Parameters:
692+
rag_chunks (list): List of RAG chunk entries containing source and metadata information.
693+
694+
Returns:
695+
list[ReferencedDocument]: ReferencedDocument instances created from the
696+
chunks; each contains `doc_url` (validated URL or `None`) and
697+
`doc_title`.
601698
"""
602699
document_entries = _process_rag_chunks_for_documents(rag_chunks)
603700
return [

src/utils/llama_stack_version.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ async def check_llama_stack_version(
3131
and maximal supported versions. Raises
3232
InvalidLlamaStackVersionException if the detected version is
3333
outside the supported range.
34+
35+
Raises:
36+
InvalidLlamaStackVersionException: If the detected version is outside
37+
the supported range or cannot be parsed.
3438
"""
3539
version_info = await client.inspect.version()
3640
compare_versions(

src/utils/mcp_headers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async def mcp_headers_dependency(request: Request) -> dict[str, dict[str, str]]:
1717
1818
mcp headers is a json dictionary or mcp url paths and their respective headers
1919
20-
Args:
20+
Parameters:
2121
request (Request): The FastAPI request object.
2222
2323
Returns:
@@ -32,7 +32,7 @@ def extract_mcp_headers(request: Request) -> dict[str, dict[str, str]]:
3232
If the header is missing, contains invalid JSON, or the decoded
3333
value is not a dictionary, an empty dictionary is returned.
3434
35-
Args:
35+
Parameters:
3636
request: The FastAPI request object
3737
3838
Returns:

src/utils/quota.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def consume_tokens(
1919
) -> None:
2020
"""Consume tokens from cluster and/or user quotas.
2121
22-
Args:
22+
Parameters:
2323
quota_limiters: List of quota limiter instances to consume tokens from.
2424
user_id: Identifier of the user consuming tokens.
2525
input_tokens: Number of input tokens to consume.
@@ -40,7 +40,7 @@ def consume_tokens(
4040
def check_tokens_available(quota_limiters: list[QuotaLimiter], user_id: str) -> None:
4141
"""Check if tokens are available for user.
4242
43-
Args:
43+
Parameters:
4444
quota_limiters: List of quota limiter instances to check.
4545
user_id: Identifier of the user to check quota for.
4646

src/utils/responses.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def extract_text_from_response_output_item(output_item: Any) -> str:
1111
formats including string content, content arrays with text parts, and refusal
1212
messages.
1313
14-
Args:
14+
Parameters:
1515
output_item: A Responses API output item (typically from response.output array).
1616
Expected to have attributes like type, role, and content.
1717

src/utils/shields.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ async def get_available_shields(client: AsyncLlamaStackClient) -> list[str]:
1414
"""
1515
Discover and return available shield identifiers.
1616
17-
Args:
17+
Parameters:
1818
client: The Llama Stack client to query for available shields.
1919
2020
Returns:
21-
List of shield identifiers that are available.
21+
list[str]: List of available shield identifiers; empty if no shields are available.
2222
"""
2323
available_shields = [shield.identifier for shield in await client.shields.list()]
2424
if not available_shields:
@@ -36,11 +36,11 @@ def detect_shield_violations(output_items: list[Any]) -> bool:
3636
attributes. If a refusal is found, increments the validation error
3737
metric and logs a warning.
3838
39-
Args:
39+
Parameters:
4040
output_items: List of output items from the LLM response to check.
4141
4242
Returns:
43-
True if a shield violation was detected, False otherwise.
43+
bool: True if a shield violation was detected, False otherwise.
4444
"""
4545
for output_item in output_items:
4646
item_type = getattr(output_item, "type", None)

src/utils/token_counter.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ class TokenCounter:
3131
llm_calls: int = 0
3232

3333
def __str__(self) -> str:
34-
"""Textual representation of TokenCounter instance."""
34+
"""
35+
Return a human-readable summary of the token usage stored in this TokenCounter.
36+
37+
Returns:
38+
summary (str): A formatted string containing `input_tokens`,
39+
`output_tokens`, `input_tokens_counted`, and `llm_calls`.
40+
"""
3541
return (
3642
f"{self.__class__.__name__}: "
3743
+ f"input_tokens: {self.input_tokens} "
@@ -47,9 +53,9 @@ def extract_token_usage_from_turn(turn: Turn, system_prompt: str = "") -> TokenC
4753
This function uses the same tokenizer and logic as the metrics system
4854
to ensure consistency between API responses and Prometheus metrics.
4955
50-
Args:
51-
turn: The turn object containing token usage information
52-
system_prompt: The system prompt used for the turn
56+
Parameters:
57+
turn (Turn): The turn object containing token usage information
58+
system_prompt (str): The system prompt used for the turn
5359
5460
Returns:
5561
TokenCounter: Token usage information
@@ -102,7 +108,7 @@ def extract_and_update_token_metrics(
102108
This function combines the token counting logic with the metrics system
103109
to ensure both API responses and Prometheus metrics are updated consistently.
104110
105-
Args:
111+
Parameters:
106112
turn: The turn object containing token usage information
107113
model: The model identifier for metrics labeling
108114
provider: The provider identifier for metrics labeling

src/utils/tool_formatter.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ def format_tool_response(tool_dict: dict[str, Any]) -> dict[str, Any]:
1919
tool_dict: Raw tool dictionary from Llama Stack
2020
2121
Returns:
22-
Formatted tool dictionary with only required fields
22+
dict[str, Any]: Formatted tool dictionary containing the following keys:
23+
- identifier: tool identifier string (defaults to "").
24+
- description: cleaned or original description string.
25+
- parameters: list of parameter definitions (defaults to empty list).
26+
- provider_id: provider identifier string (defaults to "").
27+
- toolgroup_id: tool group identifier string (defaults to "").
28+
- server_source: server source string (defaults to "").
29+
- type: tool type string (defaults to "").
2330
"""
2431
# Clean up description if it contains structured metadata
2532
description = tool_dict.get("description", "")
@@ -116,10 +123,11 @@ def format_tools_list(tools: list[dict[str, Any]]) -> list[dict[str, Any]]:
116123
"""
117124
Format a list of tools with structured description parsing.
118125
119-
Args:
120-
tools: List of raw tool dictionaries
126+
Parameters:
127+
tools: (list[dict[str, Any]]): List of raw tool dictionaries
121128
122129
Returns:
123-
List of formatted tool dictionaries
130+
list[dict[str, Any]]: Formatted tool dictionaries with normalized
131+
fields and cleaned descriptions.
124132
"""
125133
return [format_tool_response(tool) for tool in tools]

0 commit comments

Comments
 (0)