mirror of
https://github.com/meta-llama/llama-stack.git
synced 2025-12-03 09:53:45 +00:00
Some checks failed
Pre-commit / pre-commit (push) Successful in 3m27s
SqlStore Integration Tests / test-postgres (3.12) (push) Failing after 0s
Integration Auth Tests / test-matrix (oauth2_token) (push) Failing after 1s
SqlStore Integration Tests / test-postgres (3.13) (push) Failing after 0s
Integration Tests (Replay) / generate-matrix (push) Successful in 3s
Test Llama Stack Build / generate-matrix (push) Successful in 3s
Test External Providers Installed via Module / test-external-providers-from-module (venv) (push) Has been skipped
Test llama stack list-deps / generate-matrix (push) Successful in 3s
Python Package Build Test / build (3.12) (push) Failing after 4s
API Conformance Tests / check-schema-compatibility (push) Successful in 11s
Test llama stack list-deps / show-single-provider (push) Successful in 25s
Test External API and Providers / test-external (venv) (push) Failing after 34s
Vector IO Integration Tests / test-matrix (push) Failing after 43s
Test Llama Stack Build / build (push) Successful in 37s
Test Llama Stack Build / build-single-provider (push) Successful in 48s
Test llama stack list-deps / list-deps-from-config (push) Successful in 52s
Test llama stack list-deps / list-deps (push) Failing after 52s
Python Package Build Test / build (3.13) (push) Failing after 1m2s
UI Tests / ui-tests (22) (push) Successful in 1m15s
Test Llama Stack Build / build-custom-container-distribution (push) Successful in 1m29s
Unit Tests / unit-tests (3.12) (push) Failing after 1m45s
Test Llama Stack Build / build-ubi9-container-distribution (push) Successful in 1m54s
Unit Tests / unit-tests (3.13) (push) Failing after 2m13s
Integration Tests (Replay) / Integration Tests (, , , client=, ) (push) Failing after 2m20s
# What does this PR do?
This replaces the legacy "pyopenapi + strong_typing" pipeline with a
FastAPI-backed generator that has an explicit schema registry inside
`llama_stack_api`. The key changes:
1. **New generator architecture.** FastAPI now builds the OpenAPI schema
directly from the real routes, while helper modules
(`schema_collection`, `endpoints`, `schema_transforms`, etc.)
post-process the result. The old pyopenapi stack and its strong_typing
helpers are removed entirely, so we no longer rely on fragile AST
analysis or top-level import side effects.
2. **Schema registry in `llama_stack_api`.** `schema_utils.py` keeps a
`SchemaInfo` record for every `@json_schema_type`, `register_schema`,
and dynamically created request model. The OpenAPI generator and other
tooling query this registry instead of scanning the package tree,
producing deterministic names (e.g., `{MethodName}Request`), capturing
all optional/nullable fields, and making schema discovery testable. A
new unit test covers the registry behavior.
3. **Regenerated specs + CI alignment.** All docs/Stainless specs are
regenerated from the new pipeline, so optional/nullable fields now match
reality (expect the API Conformance workflow to report breaking
changes—this PR establishes the new baseline). The workflow itself is
back to the stock oasdiff invocation so future regressions surface
normally.
*Conformance will be RED on this PR; we choose to accept the
deviations.*
## Test Plan
- `uv run pytest tests/unit/server/test_schema_registry.py`
- `uv run python -m scripts.openapi_generator.main docs/static`
---------
Signed-off-by: Sébastien Han <seb@redhat.com>
Co-authored-by: Ashwin Bharambe <ashwin.bharambe@gmail.com>
229 lines
7.3 KiB
Python
229 lines
7.3 KiB
Python
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
# All rights reserved.
|
|
#
|
|
# This source code is licensed under the terms described in the LICENSE file in
|
|
# the root directory of this source tree.
|
|
|
|
from enum import Enum
|
|
from typing import Any, Literal, Protocol
|
|
|
|
from pydantic import BaseModel
|
|
from typing_extensions import runtime_checkable
|
|
|
|
from llama_stack_api.common.content_types import URL, InterleavedContent
|
|
from llama_stack_api.common.tracing import telemetry_traceable
|
|
from llama_stack_api.resource import Resource, ResourceType
|
|
from llama_stack_api.schema_utils import json_schema_type, webmethod
|
|
from llama_stack_api.version import LLAMA_STACK_API_V1
|
|
|
|
|
|
@json_schema_type
|
|
class ToolDef(BaseModel):
|
|
"""Tool definition used in runtime contexts.
|
|
|
|
:param name: Name of the tool
|
|
:param description: (Optional) Human-readable description of what the tool does
|
|
:param input_schema: (Optional) JSON Schema for tool inputs (MCP inputSchema)
|
|
:param output_schema: (Optional) JSON Schema for tool outputs (MCP outputSchema)
|
|
:param metadata: (Optional) Additional metadata about the tool
|
|
:param toolgroup_id: (Optional) ID of the tool group this tool belongs to
|
|
"""
|
|
|
|
toolgroup_id: str | None = None
|
|
name: str
|
|
description: str | None = None
|
|
input_schema: dict[str, Any] | None = None
|
|
output_schema: dict[str, Any] | None = None
|
|
metadata: dict[str, Any] | None = None
|
|
|
|
|
|
@json_schema_type
|
|
class ToolGroupInput(BaseModel):
|
|
"""Input data for registering a tool group.
|
|
|
|
:param toolgroup_id: Unique identifier for the tool group
|
|
:param provider_id: ID of the provider that will handle this tool group
|
|
:param args: (Optional) Additional arguments to pass to the provider
|
|
:param mcp_endpoint: (Optional) Model Context Protocol endpoint for remote tools
|
|
"""
|
|
|
|
toolgroup_id: str
|
|
provider_id: str
|
|
args: dict[str, Any] | None = None
|
|
mcp_endpoint: URL | None = None
|
|
|
|
|
|
@json_schema_type
|
|
class ToolGroup(Resource):
|
|
"""A group of related tools managed together.
|
|
|
|
:param type: Type of resource, always 'tool_group'
|
|
:param mcp_endpoint: (Optional) Model Context Protocol endpoint for remote tools
|
|
:param args: (Optional) Additional arguments for the tool group
|
|
"""
|
|
|
|
type: Literal[ResourceType.tool_group] = ResourceType.tool_group
|
|
mcp_endpoint: URL | None = None
|
|
args: dict[str, Any] | None = None
|
|
|
|
|
|
@json_schema_type
|
|
class ToolInvocationResult(BaseModel):
|
|
"""Result of a tool invocation.
|
|
|
|
:param content: (Optional) The output content from the tool execution
|
|
:param error_message: (Optional) Error message if the tool execution failed
|
|
:param error_code: (Optional) Numeric error code if the tool execution failed
|
|
:param metadata: (Optional) Additional metadata about the tool execution
|
|
"""
|
|
|
|
content: InterleavedContent | None = None
|
|
error_message: str | None = None
|
|
error_code: int | None = None
|
|
metadata: dict[str, Any] | None = None
|
|
|
|
|
|
class ToolStore(Protocol):
|
|
async def get_tool(self, tool_name: str) -> ToolDef: ...
|
|
async def get_tool_group(self, toolgroup_id: str) -> ToolGroup: ...
|
|
|
|
|
|
@json_schema_type
|
|
class ListToolGroupsResponse(BaseModel):
|
|
"""Response containing a list of tool groups.
|
|
|
|
:param data: List of tool groups
|
|
"""
|
|
|
|
data: list[ToolGroup]
|
|
|
|
|
|
@json_schema_type
|
|
class ListToolDefsResponse(BaseModel):
|
|
"""Response containing a list of tool definitions.
|
|
|
|
:param data: List of tool definitions
|
|
"""
|
|
|
|
data: list[ToolDef]
|
|
|
|
|
|
@runtime_checkable
|
|
@telemetry_traceable
|
|
class ToolGroups(Protocol):
|
|
@webmethod(route="/toolgroups", method="POST", level=LLAMA_STACK_API_V1, deprecated=True)
|
|
async def register_tool_group(
|
|
self,
|
|
toolgroup_id: str,
|
|
provider_id: str,
|
|
mcp_endpoint: URL | None = None,
|
|
args: dict[str, Any] | None = None,
|
|
) -> None:
|
|
"""Register a tool group.
|
|
|
|
:param toolgroup_id: The ID of the tool group to register.
|
|
:param provider_id: The ID of the provider to use for the tool group.
|
|
:param mcp_endpoint: The MCP endpoint to use for the tool group.
|
|
:param args: A dictionary of arguments to pass to the tool group.
|
|
"""
|
|
...
|
|
|
|
@webmethod(route="/toolgroups/{toolgroup_id:path}", method="GET", level=LLAMA_STACK_API_V1)
|
|
async def get_tool_group(
|
|
self,
|
|
toolgroup_id: str,
|
|
) -> ToolGroup:
|
|
"""Get a tool group by its ID.
|
|
|
|
:param toolgroup_id: The ID of the tool group to get.
|
|
:returns: A ToolGroup.
|
|
"""
|
|
...
|
|
|
|
@webmethod(route="/toolgroups", method="GET", level=LLAMA_STACK_API_V1)
|
|
async def list_tool_groups(self) -> ListToolGroupsResponse:
|
|
"""List tool groups with optional provider.
|
|
|
|
:returns: A ListToolGroupsResponse.
|
|
"""
|
|
...
|
|
|
|
@webmethod(route="/tools", method="GET", level=LLAMA_STACK_API_V1)
|
|
async def list_tools(self, toolgroup_id: str | None = None) -> ListToolDefsResponse:
|
|
"""List tools with optional tool group.
|
|
|
|
:param toolgroup_id: The ID of the tool group to list tools for.
|
|
:returns: A ListToolDefsResponse.
|
|
"""
|
|
...
|
|
|
|
@webmethod(route="/tools/{tool_name:path}", method="GET", level=LLAMA_STACK_API_V1)
|
|
async def get_tool(
|
|
self,
|
|
tool_name: str,
|
|
) -> ToolDef:
|
|
"""Get a tool by its name.
|
|
|
|
:param tool_name: The name of the tool to get.
|
|
:returns: A ToolDef.
|
|
"""
|
|
...
|
|
|
|
@webmethod(route="/toolgroups/{toolgroup_id:path}", method="DELETE", level=LLAMA_STACK_API_V1, deprecated=True)
|
|
async def unregister_toolgroup(
|
|
self,
|
|
toolgroup_id: str,
|
|
) -> None:
|
|
"""Unregister a tool group.
|
|
|
|
:param toolgroup_id: The ID of the tool group to unregister.
|
|
"""
|
|
...
|
|
|
|
|
|
class SpecialToolGroup(Enum):
|
|
"""Special tool groups with predefined functionality.
|
|
|
|
:cvar rag_tool: Retrieval-Augmented Generation tool group for document search and retrieval
|
|
"""
|
|
|
|
rag_tool = "rag_tool"
|
|
|
|
|
|
@runtime_checkable
|
|
@telemetry_traceable
|
|
class ToolRuntime(Protocol):
|
|
tool_store: ToolStore | None = None
|
|
|
|
# TODO: This needs to be renamed once OPEN API generator name conflict issue is fixed.
|
|
@webmethod(route="/tool-runtime/list-tools", method="GET", level=LLAMA_STACK_API_V1)
|
|
async def list_runtime_tools(
|
|
self,
|
|
tool_group_id: str | None = None,
|
|
mcp_endpoint: URL | None = None,
|
|
authorization: str | None = None,
|
|
) -> ListToolDefsResponse:
|
|
"""List all tools in the runtime.
|
|
|
|
:param tool_group_id: The ID of the tool group to list tools for.
|
|
:param mcp_endpoint: The MCP endpoint to use for the tool group.
|
|
:param authorization: (Optional) OAuth access token for authenticating with the MCP server.
|
|
:returns: A ListToolDefsResponse.
|
|
"""
|
|
...
|
|
|
|
@webmethod(route="/tool-runtime/invoke", method="POST", level=LLAMA_STACK_API_V1)
|
|
async def invoke_tool(
|
|
self,
|
|
tool_name: str,
|
|
kwargs: dict[str, Any],
|
|
authorization: str | None = None,
|
|
) -> ToolInvocationResult:
|
|
"""Run a tool with the given arguments.
|
|
|
|
:param tool_name: The name of the tool to invoke.
|
|
:param kwargs: A dictionary of arguments to pass to the tool.
|
|
:param authorization: (Optional) OAuth access token for authenticating with the MCP server.
|
|
:returns: A ToolInvocationResult.
|
|
"""
|
|
...
|