Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
feat: create `model` table to persist available models (#5835)
Sarah Wooders982501f6faf5
diff --git a/.github/workflows/core-unit-test.yml b/.github/workflows/core-unit-test.ymlindex 54a18cf71..9b633b81b 100644--- a/.github/workflows/core-unit-test.yml+++ b/.github/workflows/core-unit-test.yml@@ -47,6 +47,7 @@ jobs:{"test_suite": "test_llm_clients.py"},{"test_suite": "test_letta_agent_batch.py"},{"test_suite": "test_providers.py"},+ {"test_suite": "test_server_providers.py"},{"test_suite": "test_sources.py"},{"test_suite": "sdk/"},{"test_suite": "mcp_tests/"},diff --git a/alembic/versions/2dbb2cf49e07_add_models_table.py b/alembic/versions/2dbb2cf49e07_add_models_table.pynew file mode 100644index 000000000..74ae3a104--- /dev/null+++ b/alembic/versions/2dbb2cf49e07_add_models_table.py@@ -0,0 +1,66 @@+"""add models table++Revision ID: 2dbb2cf49e07+Revises: a1b2c3d4e5f6+Create Date: 2025-11-06 14:49:10.902099++"""++from typing import Sequence, Union++import sqlalchemy as sa++from alembic import op++# revision identifiers, used by Alembic.+revision: str = "2dbb2cf49e07"+down_revision: Union[str, None] = "a1b2c3d4e5f6"+branch_labels: Union[str, Sequence[str], None] = None+depends_on: Union[str, Sequence[str], None] = None+++def upgrade() -> None:+ # ### commands auto generated by Alembic - please adjust! ###+ op.create_table(+ "provider_models",+ sa.Column("handle", sa.String(), nullable=False),+ sa.Column("display_name", sa.String(), nullable=False),+ sa.Column("name", sa.String(), nullable=False),+ sa.Column("provider_id", sa.String(), nullable=False),+ sa.Column("organization_id", sa.String(), nullable=True),+ sa.Column("model_type", sa.String(), nullable=False),+ sa.Column("enabled", sa.Boolean(), server_default="TRUE", nullable=False),+ sa.Column("model_endpoint_type", sa.String(), nullable=False),+ sa.Column("max_context_window", sa.Integer(), nullable=True),+ sa.Column("supports_token_streaming", sa.Boolean(), nullable=True),+ sa.Column("supports_tool_calling", sa.Boolean(), nullable=True),+ sa.Column("embedding_dim", sa.Integer(), nullable=True),+ sa.Column("id", sa.String(), nullable=False),+ sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=True),+ sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=True),+ sa.Column("is_deleted", sa.Boolean(), server_default=sa.text("FALSE"), nullable=False),+ sa.Column("_created_by_id", sa.String(), nullable=True),+ sa.Column("_last_updated_by_id", sa.String(), nullable=True),+ sa.ForeignKeyConstraint(["organization_id"], ["organizations.id"], ondelete="CASCADE"),+ sa.ForeignKeyConstraint(["provider_id"], ["providers.id"], ondelete="CASCADE"),+ sa.PrimaryKeyConstraint("id"),+ sa.UniqueConstraint("handle", "organization_id", "model_type", name="unique_handle_per_org_and_type"),+ sa.UniqueConstraint("name", "provider_id", "model_type", name="unique_model_per_provider_and_type"),+ )+ op.create_index(op.f("ix_provider_models_handle"), "provider_models", ["handle"], unique=False)+ op.create_index(op.f("ix_provider_models_model_type"), "provider_models", ["model_type"], unique=False)+ op.create_index(op.f("ix_provider_models_organization_id"), "provider_models", ["organization_id"], unique=False)+ op.create_index(op.f("ix_provider_models_provider_id"), "provider_models", ["provider_id"], unique=False)+ op.alter_column("providers", "organization_id", existing_type=sa.VARCHAR(), nullable=True)+ # ### end Alembic commands ###+++def downgrade() -> None:+ # ### commands auto generated by Alembic - please adjust! ###+ op.alter_column("providers", "organization_id", existing_type=sa.VARCHAR(), nullable=False)+ op.drop_index(op.f("ix_provider_models_provider_id"), table_name="provider_models")+ op.drop_index(op.f("ix_provider_models_organization_id"), table_name="provider_models")+ op.drop_index(op.f("ix_provider_models_model_type"), table_name="provider_models")+ op.drop_index(op.f("ix_provider_models_handle"), table_name="provider_models")+ op.drop_table("provider_models")+ # ### end Alembic commands ###diff --git a/letta/orm/__init__.py b/letta/orm/__init__.pyindex a834ab90b..b9fae4516 100644--- a/letta/orm/__init__.py+++ b/letta/orm/__init__.py@@ -25,6 +25,7 @@ from letta.orm.passage import ArchivalPassage, BasePassage, SourcePassagefrom letta.orm.passage_tag import PassageTagfrom letta.orm.prompt import Promptfrom letta.orm.provider import Provider+from letta.orm.provider_model import ProviderModelfrom letta.orm.provider_trace import ProviderTracefrom letta.orm.run import Runfrom letta.orm.run_metrics import RunMetricsdiff --git a/letta/orm/organization.py b/letta/orm/organization.pyindex d6d9cbdff..c24f5553e 100644--- a/letta/orm/organization.py+++ b/letta/orm/organization.py@@ -19,6 +19,7 @@ if TYPE_CHECKING:from letta.orm.passage import ArchivalPassage, SourcePassagefrom letta.orm.passage_tag import PassageTagfrom letta.orm.provider import Provider+ from letta.orm.provider_model import ProviderModelfrom letta.orm.provider_trace import ProviderTracefrom letta.orm.run import Runfrom letta.orm.sandbox_config import AgentEnvironmentVariable, SandboxConfig, SandboxEnvironmentVariable@@ -63,6 +64,9 @@ class Organization(SqlalchemyBase):passage_tags: Mapped[List["PassageTag"]] = relationship("PassageTag", back_populates="organization", cascade="all, delete-orphan")archives: Mapped[List["Archive"]] = relationship("Archive", back_populates="organization", cascade="all, delete-orphan")providers: Mapped[List["Provider"]] = relationship("Provider", back_populates="organization", cascade="all, delete-orphan")+ provider_models: Mapped[List["ProviderModel"]] = relationship(+ "ProviderModel", back_populates="organization", cascade="all, delete-orphan"+ )identities: Mapped[List["Identity"]] = relationship("Identity", back_populates="organization", cascade="all, delete-orphan")groups: Mapped[List["Group"]] = relationship("Group", back_populates="organization", cascade="all, delete-orphan")llm_batch_jobs: Mapped[List["LLMBatchJob"]] = relationship("LLMBatchJob", back_populates="organization", cascade="all, delete-orphan")diff --git a/letta/orm/provider.py b/letta/orm/provider.pyindex c6a3cadcf..bd42a1bea 100644--- a/letta/orm/provider.py+++ b/letta/orm/provider.py@@ -1,6 +1,6 @@from typing import TYPE_CHECKING, Optional-from sqlalchemy import Text, UniqueConstraint+from sqlalchemy import ForeignKey, String, Text, UniqueConstraintfrom sqlalchemy.orm import Mapped, mapped_column, relationshipfrom letta.orm.mixins import OrganizationMixin@@ -9,6 +9,7 @@ from letta.schemas.providers import Provider as PydanticProviderif TYPE_CHECKING:from letta.orm.organization import Organization+ from letta.orm.provider_model import ProviderModelclass Provider(SqlalchemyBase, OrganizationMixin):@@ -24,6 +25,9 @@ class Provider(SqlalchemyBase, OrganizationMixin):),)+ # Override organization_id to make it nullable for base providers+ organization_id: Mapped[Optional[str]] = mapped_column(String, ForeignKey("organizations.id"), nullable=True)+name: Mapped[str] = mapped_column(nullable=False, doc="The name of the provider")provider_type: Mapped[str] = mapped_column(nullable=True, doc="The type of the provider")provider_category: Mapped[str] = mapped_column(nullable=True, doc="The category of the provider (base or byok)")@@ -39,3 +43,4 @@ class Provider(SqlalchemyBase, OrganizationMixin):# relationshipsorganization: Mapped["Organization"] = relationship("Organization", back_populates="providers")+ models: Mapped[list["ProviderModel"]] = relationship("ProviderModel", back_populates="provider", cascade="all, delete-orphan")diff --git a/letta/orm/provider_model.py b/letta/orm/provider_model.pynew file mode 100644index 000000000..6e5ed0236--- /dev/null+++ b/letta/orm/provider_model.py@@ -0,0 +1,75 @@+from typing import TYPE_CHECKING, Optional++from sqlalchemy import Boolean, ForeignKey, String, UniqueConstraint+from sqlalchemy.orm import Mapped, mapped_column, relationship++from letta.orm.sqlalchemy_base import SqlalchemyBase+from letta.schemas.provider_model import ProviderModel as PydanticProviderModel++if TYPE_CHECKING:+ from letta.orm.organization import Organization+ from letta.orm.provider import Provider+++class ProviderModel(SqlalchemyBase):+ """ProviderModel ORM class - represents individual models available from providers"""++ __tablename__ = "provider_models"+ __pydantic_model__ = PydanticProviderModel+ __table_args__ = (+ UniqueConstraint(+ "handle",+ "organization_id",+ "model_type",+ name="unique_handle_per_org_and_type",+ ),+ UniqueConstraint(+ "name",+ "provider_id",+ "model_type",+ name="unique_model_per_provider_and_type",+ ),+ )++ # The unique handle used in the API (e.g., "openai/gpt-4o-mini", "anthropic/claude-3-5-sonnet")+ # Format: {provider_name}/{display_name}+ handle: Mapped[str] = mapped_column(String, nullable=False, index=True, doc="Unique handle for API reference")++ # Display name shown in the UI for the model+ display_name: Mapped[str] = mapped_column(String, nullable=False, doc="Display name for the model")++ # The actual model name used by the provider (e.g., "gpt-4o-mini", "openai/gpt-4" for OpenRouter)+ name: Mapped[str] = mapped_column(String, nullable=False, doc="The actual model name used by the provider")++ # Foreign key to the provider+ provider_id: Mapped[str] = mapped_column(+ String, ForeignKey("providers.id", ondelete="CASCADE"), nullable=False, index=True, doc="Provider ID reference"+ )++ # Optional organization ID - NULL for global models, set for org-scoped models+ organization_id: Mapped[Optional[str]] = mapped_column(+ String,+ ForeignKey("organizations.id", ondelete="CASCADE"),+ nullable=True,+ index=True,+ doc="Organization ID if org-scoped, NULL if global",+ )++ # Model type: llm or embedding+ model_type: Mapped[str] = mapped_column(String, nullable=False, index=True, doc="Type of model (llm or embedding)")++ # Whether the model is enabled (default True)+ enabled: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True, server_default="TRUE", doc="Whether the model is enabled")++ # Model endpoint type (e.g., "openai", "anthropic", etc.)+ model_endpoint_type: Mapped[str] = mapped_column(String, nullable=False, doc="The endpoint type for the model")++ # Additional metadata fields+ max_context_window: Mapped[int] = mapped_column(nullable=True, doc="Context window size for the model")+ supports_token_streaming: Mapped[bool] = mapped_column(Boolean, nullable=True, doc="Whether streaming is supported")+ supports_tool_calling: Mapped[bool] = mapped_column(Boolean, nullable=True, doc="Whether tool calling is supported")+ embedding_dim: Mapped[Optional[int]] = mapped_column(nullable=True, doc="Embedding dimension for embedding models")++ # relationships+ provider: Mapped["Provider"] = relationship("Provider", back_populates="models")+ organization: Mapped[Optional["Organization"]] = relationship("Organization", back_populates="provider_models")diff --git a/letta/schemas/enums.py b/letta/schemas/enums.pyindex 6cb3ba2ff..da4dc27f5 100644--- a/letta/schemas/enums.py+++ b/letta/schemas/enums.py@@ -22,6 +22,7 @@ class PrimitiveType(str, Enum):ARCHIVE = "archive"PASSAGE = "passage"PROVIDER = "provider"+ PROVIDER_MODEL = "model"SANDBOX_CONFIG = "sandbox" # Note: sandbox_config IDs use "sandbox" prefixSTEP = "step"IDENTITY = "identity"diff --git a/letta/schemas/provider_model.py b/letta/schemas/provider_model.pynew file mode 100644index 000000000..fd948fd80--- /dev/null+++ b/letta/schemas/provider_model.py@@ -0,0 +1,77 @@+from datetime import datetime+from typing import Optional++from pydantic import Field++from letta.schemas.enums import PrimitiveType+from letta.schemas.letta_base import OrmMetadataBase+++class ProviderModelBase(OrmMetadataBase):+ __id_prefix__ = PrimitiveType.PROVIDER_MODEL.value+++class ProviderModel(ProviderModelBase):+ """+ Pydantic model for provider models.++ This represents individual models available from providers with a unique handle+ that decouples the user-facing API from provider-specific implementation details.+ """++ id: str = ProviderModelBase.generate_id_field()++ # The unique handle used in the API (e.g., "openai/gpt-4o-mini", "anthropic/claude-3-5-sonnet")+ # Format: {provider_display_name}/{model_display_name}+ handle: str = Field(..., description="Unique handle for API reference (format: provider_display_name/model_display_name)")++ # Display name shown in the UI for the model+ name: str = Field(..., description="The actual model name used by the provider")+ display_name: str = Field(..., description="Display name for the model shown in UI")++ # Foreign key to the provider+ provider_id: str = Field(..., description="Provider ID reference")++ # Optional organization ID - NULL for global models, set for org-scoped models+ organization_id: Optional[str] = Field(None, description="Organization ID if org-scoped, NULL if global")++ # Model type: llm or embedding+ model_type: str = Field(..., description="Type of model (llm or embedding)")++ # Whether the model is enabled (default True)+ enabled: bool = Field(default=True, description="Whether the model is enabled")++ # Model endpoint type (e.g., "openai", "anthropic", etc.)+ model_endpoint_type: str = Field(..., description="The endpoint type for the model (e.g., 'openai', 'anthropic')")++ # Additional metadata fields+ max_context_window: Optional[int] = Field(None, description="Context window size for the model")+ supports_token_streaming: Optional[bool] = Field(None, description="Whether token streaming is supported")+ supports_tool_calling: Optional[bool] = Field(None, description="Whether tool calling is supported")+ embedding_dim: Optional[int] = Field(None, description="Embedding dimension for embedding models")+++class ProviderModelCreate(ProviderModelBase):+ """Schema for creating a new provider model"""++ handle: str = Field(..., description="Unique handle for API reference (format: provider_display_name/model_display_name)")+ display_name: str = Field(..., description="Display name for the model shown in UI")+ model_name: str = Field(..., description="The actual model name used by the provider")+ model_display_name: str = Field(..., description="Model display name used in the handle")+ provider_display_name: str = Field(..., description="Display name for the provider")+ provider_id: str = Field(..., description="Provider ID reference")+ model_type: str = Field(..., description="Type of model (llm or embedding)")+ enabled: bool = Field(default=True, description="Whether the model is enabled")+ context_window: Optional[int] = Field(None, description="Context window size for the model")+ supports_streaming: Optional[bool] = Field(None, description="Whether streaming is supported")+ supports_function_calling: Optional[bool] = Field(None, description="Whether function calling is supported")+++class ProviderModelUpdate(ProviderModelBase):+ """Schema for updating a provider model"""++ display_name: Optional[str] = Field(None, description="Display name for the model shown in UI")+ enabled: Optional[bool] = Field(None, description="Whether the model is enabled")+ context_window: Optional[int] = Field(None, description="Context window size for the model")+ supports_streaming: Optional[bool] = Field(None, description="Whether streaming is supported")+ supports_function_calling: Optional[bool] = Field(None, description="Whether function calling is supported")diff --git a/letta/server/rest_api/routers/v1/agents.py b/letta/server/rest_api/routers/v1/agents.pyindex 9683803aa..ec54fe97a 100644--- a/letta/server/rest_api/routers/v1/agents.py+++ b/letta/server/rest_api/routers/v1/agents.py@@ -305,7 +305,7 @@ async def _import_agent(agent_schema = AgentFileSchema.model_validate(agent_file_json)if override_embedding_handle:- embedding_config_override = await server.get_cached_embedding_config_async(actor=actor, handle=override_embedding_handle)+ embedding_config_override = await server.get_embedding_config_from_handle_async(actor=actor, handle=override_embedding_handle)else:embedding_config_override = Nonediff --git a/letta/server/rest_api/routers/v1/providers.py b/letta/server/rest_api/routers/v1/providers.pyindex 39cdd0043..f15397976 100644--- a/letta/server/rest_api/routers/v1/providers.py+++ b/letta/server/rest_api/routers/v1/providers.py@@ -72,9 +72,9 @@ async def create_provider(if isinstance(value, str) and value == "":setattr(request, field_name, None)- request_data = request.model_dump(exclude_unset=True, exclude_none=True)- provider = ProviderCreate(**request_data)- provider = await server.provider_manager.create_provider_async(provider, actor=actor)+ # ProviderCreate no longer has provider_category field+ # API-created providers are always BYOK (bring your own key)+ provider = await server.provider_manager.create_provider_async(request, actor=actor, is_byok=True)return providerdiff --git a/letta/server/rest_api/routers/v1/tools.py b/letta/server/rest_api/routers/v1/tools.pyindex 490c0698b..894ba6aae 100644--- a/letta/server/rest_api/routers/v1/tools.py+++ b/letta/server/rest_api/routers/v1/tools.py@@ -818,7 +818,7 @@ async def generate_tool_from_prompt(Generate a tool from the given user prompt."""actor = await server.user_manager.get_actor_or_default_async(actor_id=headers.actor_id)- llm_config = await server.get_cached_llm_config_async(actor=actor, handle=request.handle or DEFAULT_GENERATE_TOOL_MODEL_HANDLE)+ llm_config = await server.get_llm_config_from_handle_async(actor=actor, handle=request.handle or DEFAULT_GENERATE_TOOL_MODEL_HANDLE)formatted_prompt = (f"Generate a python function named {request.tool_name} using the instructions below "+ (f"based on this starter code: \n\n```\n{request.starter_code}\n```\n\n" if request.starter_code else "\n")diff --git a/letta/services/provider_manager.py b/letta/services/provider_manager.pyindex 907bdc495..f99f79445 100644--- a/letta/services/provider_manager.py+++ b/letta/services/provider_manager.py@@ -1,8 +1,12 @@from typing import List, Optional, Tuple, Unionfrom letta.orm.provider import Provider as ProviderModel+from letta.orm.provider_model import ProviderModel as ProviderModelORMfrom letta.otel.tracing import trace_method+from letta.schemas.embedding_config import EmbeddingConfigfrom letta.schemas.enums import PrimitiveType, ProviderCategory, ProviderType+from letta.schemas.llm_config import LLMConfig+from letta.schemas.provider_model import ProviderModel as PydanticProviderModelfrom letta.schemas.providers import Provider as PydanticProvider, ProviderCheck, ProviderCreate, ProviderUpdatefrom letta.schemas.secret import Secretfrom letta.schemas.user import User as PydanticUser@@ -14,17 +18,54 @@ from letta.validators import raise_on_invalid_idclass ProviderManager:@enforce_types@trace_method- async def create_provider_async(self, request: ProviderCreate, actor: PydanticUser) -> PydanticProvider:- """Create a new provider if it doesn't already exist."""- async with db_registry.async_session() as session:- provider_create_args = {**request.model_dump(), "provider_category": ProviderCategory.byok}- provider = PydanticProvider(**provider_create_args)-- if provider.name == provider.provider_type.value:- raise ValueError("Provider name must be unique and different from provider type")+ async def create_provider_async(self, request: ProviderCreate, actor: PydanticUser, is_byok: bool = True) -> PydanticProvider:+ """Create a new provider if it doesn't already exist.- # Assign the organization id based on the actor- provider.organization_id = actor.organization_id+ Args:+ request: ProviderCreate object with provider details+ actor: User creating the provider+ is_byok: If True, creates a BYOK provider (default). If False, creates a base provider.+ """+ async with db_registry.async_session() as session:+ from letta.schemas.enums import ProviderCategory++ # Check for name conflicts+ if is_byok:+ # BYOK providers cannot use the same name as base providers+ existing_base_providers = await ProviderModel.list_async(+ db_session=session,+ name=request.name,+ organization_id=None, # Base providers have NULL organization_id+ limit=1,+ )+ if existing_base_providers:+ raise ValueError(+ f"Provider name '{request.name}' conflicts with an existing base provider. Please choose a different name."+ )+ else:+ # Base providers must have unique names among themselves+ # (the DB constraint won't catch this because NULL != NULL)+ existing_base_providers = await ProviderModel.list_async(+ db_session=session,+ name=request.name,+ organization_id=None, # Base providers have NULL organization_id+ limit=1,+ )+ if existing_base_providers:+ raise ValueError(f"Base provider name '{request.name}' already exists. Please choose a different name.")++ # Create provider with the appropriate category+ provider_data = request.model_dump()+ provider_data["provider_category"] = ProviderCategory.byok if is_byok else ProviderCategory.base+ provider = PydanticProvider(**provider_data)++ # if provider.name == provider.provider_type.value:+ # raise ValueError("Provider name must be unique and different from provider type")++ # Only assign organization id for non-base providers+ # Base providers should be globally accessible (org_id = None)+ if is_byok:+ provider.organization_id = actor.organization_id# Lazily create the provider id prior to persistenceprovider.resolve_identifier()@@ -37,7 +78,13 @@ class ProviderManager:new_provider = ProviderModel(**provider.model_dump(to_orm=True, exclude_unset=True))await new_provider.create_async(session, actor=actor)- return new_provider.to_pydantic()+ provider_pydantic = new_provider.to_pydantic()++ # For BYOK providers, automatically sync available models+ if is_byok:+ await self._sync_default_models_for_provider(provider_pydantic, actor)++ return provider_pydantic@enforce_types@trace_method@@ -135,6 +182,7 @@ class ProviderManager:) -> List[PydanticProvider]:"""List all providers with pagination support.+ Returns both global providers (organization_id=NULL) and organization-specific providers."""filter_kwargs = {}if name:@@ -142,7 +190,59 @@ class ProviderManager:if provider_type:filter_kwargs["provider_type"] = provider_typeasync with db_registry.async_session() as session:- providers = await ProviderModel.list_async(+ # Get organization-specific providers+ org_providers = await ProviderModel.list_async(+ db_session=session,+ before=before,+ after=after,+ limit=limit,+ actor=actor,+ ascending=ascending,+ check_is_deleted=True,+ **filter_kwargs,+ )++ # Get global providers (base providers with organization_id=NULL)+ global_filter_kwargs = {**filter_kwargs, "organization_id": None}+ global_providers = await ProviderModel.list_async(+ db_session=session,+ before=before,+ after=after,+ limit=limit,+ ascending=ascending,+ check_is_deleted=True,+ **global_filter_kwargs,+ )++ # Combine both lists+ all_providers = org_providers + global_providers++ return [provider.to_pydantic() for provider in all_providers]++ @enforce_types+ @trace_method+ def list_providers(+ self,+ actor: PydanticUser,+ name: Optional[str] = None,+ provider_type: Optional[ProviderType] = None,+ before: Optional[str] = None,+ after: Optional[str] = None,+ limit: Optional[int] = 50,+ ascending: bool = False,+ ) -> List[PydanticProvider]:+ """+ List all providers with pagination support (synchronous version).+ Returns both global providers (organization_id=NULL) and organization-specific providers.+ """+ filter_kwargs = {}+ if name:+ filter_kwargs["name"] = name+ if provider_type:+ filter_kwargs["provider_type"] = provider_type+ with db_registry.get_session() as session:+ # Get organization-specific providers+ org_providers = ProviderModel.list(db_session=session,before=before,after=after,@@ -152,15 +252,50 @@ class ProviderManager:check_is_deleted=True,**filter_kwargs,)- return [provider.to_pydantic() for provider in providers]++ # Get global providers (base providers with organization_id=NULL)+ global_filter_kwargs = {**filter_kwargs, "organization_id": None}+ global_providers = ProviderModel.list(+ db_session=session,+ before=before,+ after=after,+ limit=limit,+ ascending=ascending,+ check_is_deleted=True,+ **global_filter_kwargs,+ )++ # Combine both lists+ all_providers = org_providers + global_providers++ return [provider.to_pydantic() for provider in all_providers]@enforce_types@trace_method@raise_on_invalid_id(param_name="provider_id", expected_prefix=PrimitiveType.PROVIDER)async def get_provider_async(self, provider_id: str, actor: PydanticUser) -> PydanticProvider:async with db_registry.async_session() as session:- provider_model = await ProviderModel.read_async(db_session=session, identifier=provider_id, actor=actor)- return provider_model.to_pydantic()+ # First try to get as organization-specific provider+ try:+ provider_model = await ProviderModel.read_async(db_session=session, identifier=provider_id, actor=actor)+ return provider_model.to_pydantic()+ except:+ # If not found, try to get as global provider (organization_id=NULL)+ from sqlalchemy import select++ stmt = select(ProviderModel).where(+ ProviderModel.id == provider_id,+ ProviderModel.organization_id.is_(None),+ ProviderModel.is_deleted == False,+ )+ result = await session.execute(stmt)+ provider_model = result.scalar_one_or_none()+ if provider_model:+ return provider_model.to_pydantic()+ else:+ from letta.orm.errors import NoResultFound++ raise NoResultFound(f"Provider not found with id='{provider_id}'")@enforce_types@trace_method@@ -253,3 +388,501 @@ class ProviderManager:raise ValueError("API key is required!")await provider.check_api_key()++ async def _sync_default_models_for_provider(self, provider: PydanticProvider, actor: PydanticUser) -> None:+ """Sync models for a newly created BYOK provider by querying the provider's API."""+ from letta.log import get_logger++ logger = get_logger(__name__)++ try:+ # Get the provider class and create an instance+ from letta.schemas.providers.anthropic import AnthropicProvider+ from letta.schemas.providers.azure import AzureProvider+ from letta.schemas.providers.bedrock import BedrockProvider+ from letta.schemas.providers.google_gemini import GoogleAIProvider+ from letta.schemas.providers.groq import GroqProvider+ from letta.schemas.providers.ollama import OllamaProvider+ from letta.schemas.providers.openai import OpenAIProvider++ provider_type_to_class = {+ "openai": OpenAIProvider,+ "anthropic": AnthropicProvider,+ "groq": GroqProvider,+ "google": GoogleAIProvider,+ "ollama": OllamaProvider,+ "bedrock": BedrockProvider,+ "azure": AzureProvider,+ }++ provider_type = provider.provider_type.value if hasattr(provider.provider_type, "value") else str(provider.provider_type)+ provider_class = provider_type_to_class.get(provider_type)++ if not provider_class:+ logger.warning(f"No provider class found for type '{provider_type}'")+ return++ # Create provider instance with necessary parameters+ kwargs = {+ "name": provider.name,+ "api_key": provider.api_key,+ "provider_category": provider.provider_category,+ }+ if provider.base_url:+ kwargs["base_url"] = provider.base_url+ if provider.access_key:+ kwargs["access_key"] = provider.access_key+ if provider.region:+ kwargs["region"] = provider.region+ if provider.api_version:+ kwargs["api_version"] = provider.api_version++ provider_instance = provider_class(**kwargs)++ # Query the provider's API for available models+ llm_models = await provider_instance.list_llm_models_async()+ embedding_models = await provider_instance.list_embedding_models_async()++ # Update handles and provider_name for BYOK providers+ for model in llm_models:+ model.provider_name = provider.name+ model.handle = f"{provider.name}/{model.model}"+ model.provider_category = provider.provider_category++ for model in embedding_models:+ model.handle = f"{provider.name}/{model.embedding_model}"++ # Use existing sync_provider_models_async to save to database+ await self.sync_provider_models_async(+ provider=provider, llm_models=llm_models, embedding_models=embedding_models, organization_id=actor.organization_id+ )++ except Exception as e:+ logger.error(f"Failed to sync models for provider '{provider.name}': {e}")+ # Don't fail provider creation if model sync fails++ @enforce_types+ @trace_method+ async def sync_base_providers(self, base_providers: list[PydanticProvider], actor: PydanticUser) -> None:+ """+ Sync base providers (from environment) to database (idempotent).++ This method is safe to call from multiple pods simultaneously as it:+ 1. Checks if provider exists before creating+ 2. Handles race conditions with UniqueConstraintViolationError+ 3. Only creates providers that don't exist (no updates to avoid conflicts)++ Args:+ base_providers: List of base provider instances from environment variables+ actor: User actor for database operations+ """+ from letta.log import get_logger+ from letta.orm.errors import UniqueConstraintViolationError++ logger = get_logger(__name__)+ logger.info(f"Syncing {len(base_providers)} base providers to database")++ async with db_registry.async_session() as session:+ for provider in base_providers:+ try:+ # Check if base provider already exists (base providers have organization_id=None)+ existing_providers = await ProviderModel.list_async(+ db_session=session,+ name=provider.name,+ organization_id=None, # Base providers are global+ limit=1,+ )++ if existing_providers:+ logger.debug(f"Base provider '{provider.name}' already exists in database, skipping")+ continue++ # Convert Provider to ProviderCreate+ provider_create = ProviderCreate(+ name=provider.name,+ provider_type=provider.provider_type,+ api_key=provider.api_key or "", # ProviderCreate requires api_key, use empty string if None+ access_key=provider.access_key,+ region=provider.region,+ base_url=provider.base_url,+ api_version=provider.api_version,+ )++ # Create the provider in the database as a base provider+ await self.create_provider_async(request=provider_create, actor=actor, is_byok=False)+ logger.info(f"Successfully initialized base provider '{provider.name}' to database")++ except UniqueConstraintViolationError:+ # Race condition: another pod created this provider between our check and create+ # This is expected and safe - just log and continue+ logger.debug(f"Provider '{provider.name}' was created by another pod, skipping")+ except Exception as e:+ # Log error but don't fail startup - provider initialization is not critical+ logger.error(f"Failed to sync provider '{provider.name}' to database: {e}", exc_info=True)++ @enforce_types+ @trace_method+ async def sync_provider_models_async(+ self,+ provider: PydanticProvider,+ llm_models: List[LLMConfig],+ embedding_models: List[EmbeddingConfig],+ organization_id: Optional[str] = None,+ ) -> None:+ """Sync models from a provider to the database - adds new models and removes old ones."""+ from letta.log import get_logger++ logger = get_logger(__name__)+ logger.info(f"=== Starting sync for provider '{provider.name}' (ID: {provider.id}) ===")+ logger.info(f" Organization ID: {organization_id}")+ logger.info(f" LLM models to sync: {[m.handle for m in llm_models]}")+ logger.info(f" Embedding models to sync: {[m.handle for m in embedding_models]}")++ async with db_registry.async_session() as session:+ # Get all existing models for this provider and organization+ # We need to handle None organization_id specially for SQL NULL comparisons+ from sqlalchemy import and_, select++ # Build the query conditions+ if organization_id is None:+ # For global models (organization_id IS NULL), excluding soft-deleted+ stmt = select(ProviderModelORM).where(+ and_(+ ProviderModelORM.provider_id == provider.id,+ ProviderModelORM.organization_id.is_(None),+ ProviderModelORM.is_deleted == False, # Filter out soft-deleted models+ )+ )+ result = await session.execute(stmt)+ existing_models = list(result.scalars().all())+ else:+ # For org-specific models+ existing_models = await ProviderModelORM.list_async(+ db_session=session,+ check_is_deleted=True, # Filter out soft-deleted models+ **{+ "provider_id": provider.id,+ "organization_id": organization_id,+ },+ )++ # Build sets of handles for incoming models+ incoming_llm_handles = {llm.handle for llm in llm_models}+ incoming_embedding_handles = {emb.handle for emb in embedding_models}+ all_incoming_handles = incoming_llm_handles | incoming_embedding_handles++ # Determine which models to remove (existing models not in the incoming list)+ models_to_remove = []+ for existing_model in existing_models:+ if existing_model.handle not in all_incoming_handles:+ models_to_remove.append(existing_model)++ # Remove models that are no longer in the sync list+ for model_to_remove in models_to_remove:+ await model_to_remove.delete_async(session)+ logger.debug(f"Removed model {model_to_remove.handle} from provider {provider.name}")++ # Commit the deletions+ await session.commit()++ # Process LLM models - add new ones+ logger.info(f"Processing {len(llm_models)} LLM models for provider {provider.name}")+ for llm_config in llm_models:+ logger.info(f" Checking LLM model: {llm_config.handle} (name: {llm_config.model})")++ # Check if model already exists (excluding soft-deleted ones)+ existing = await ProviderModelORM.list_async(+ db_session=session,+ limit=1,+ check_is_deleted=True, # Filter out soft-deleted models+ **{+ "handle": llm_config.handle,+ "organization_id": organization_id,+ "model_type": "llm", # Must check model_type since handle can be same for LLM and embedding+ },+ )++ if not existing:+ logger.info(f" Creating new LLM model {llm_config.handle}")+ # Create new model entry+ pydantic_model = PydanticProviderModel(+ handle=llm_config.handle,+ display_name=llm_config.model,+ name=llm_config.model,+ provider_id=provider.id,+ organization_id=organization_id,+ model_type="llm",+ enabled=True,+ model_endpoint_type=llm_config.model_endpoint_type,+ max_context_window=llm_config.context_window,+ supports_token_streaming=llm_config.model_endpoint_type in ["openai", "anthropic", "deepseek"],+ supports_tool_calling=True, # Assume true for LLMs for now+ )++ logger.info(+ f" Model data: handle={pydantic_model.handle}, name={pydantic_model.name}, "+ f"model_type={pydantic_model.model_type}, provider_id={pydantic_model.provider_id}, "+ f"org_id={pydantic_model.organization_id}"+ )++ # Convert to ORM+ model = ProviderModelORM(**pydantic_model.model_dump(to_orm=True))+ try:+ await model.create_async(session)+ logger.info(f" ✓ Successfully created LLM model {llm_config.handle} with ID {model.id}")+ except Exception as e:+ logger.error(f" ✗ Failed to create LLM model {llm_config.handle}: {e}")+ # Log the full error details+ import traceback++ logger.error(f" Full traceback: {traceback.format_exc()}")+ # Roll back the session to clear the failed transaction+ await session.rollback()+ else:+ logger.info(f" LLM model {llm_config.handle} already exists (ID: {existing[0].id}), skipping")++ # Process embedding models - add new ones+ logger.info(f"Processing {len(embedding_models)} embedding models for provider {provider.name}")+ for embedding_config in embedding_models:+ logger.info(f" Checking embedding model: {embedding_config.handle} (name: {embedding_config.embedding_model})")++ # Check if model already exists (excluding soft-deleted ones)+ existing = await ProviderModelORM.list_async(+ db_session=session,+ limit=1,+ check_is_deleted=True, # Filter out soft-deleted models+ **{+ "handle": embedding_config.handle,+ "organization_id": organization_id,+ "model_type": "embedding", # Must check model_type since handle can be same for LLM and embedding+ },+ )++ if not existing:+ logger.info(f" Creating new embedding model {embedding_config.handle}")+ # Create new model entry+ pydantic_model = PydanticProviderModel(+ handle=embedding_config.handle,+ display_name=embedding_config.embedding_model,+ name=embedding_config.embedding_model,+ provider_id=provider.id,+ organization_id=organization_id,+ model_type="embedding",+ enabled=True,+ model_endpoint_type=embedding_config.embedding_endpoint_type,+ embedding_dim=embedding_config.embedding_dim if hasattr(embedding_config, "embedding_dim") else None,+ )++ logger.info(+ f" Model data: handle={pydantic_model.handle}, name={pydantic_model.name}, "+ f"model_type={pydantic_model.model_type}, provider_id={pydantic_model.provider_id}, "+ f"org_id={pydantic_model.organization_id}"+ )++ # Convert to ORM+ model = ProviderModelORM(**pydantic_model.model_dump(to_orm=True))+ try:+ await model.create_async(session)+ logger.info(f" ✓ Successfully created embedding model {embedding_config.handle} with ID {model.id}")+ except Exception as e:+ logger.error(f" ✗ Failed to create embedding model {embedding_config.handle}: {e}")+ # Log the full error details+ import traceback++ logger.error(f" Full traceback: {traceback.format_exc()}")+ # Roll back the session to clear the failed transaction+ await session.rollback()+ else:+ logger.info(f" Embedding model {embedding_config.handle} already exists (ID: {existing[0].id}), skipping")++ @enforce_types+ @trace_method+ async def get_model_by_handle_async(+ self,+ handle: str,+ actor: PydanticUser,+ model_type: Optional[str] = None,+ ) -> Optional[PydanticProviderModel]:+ """Get a model by its handle. Handles are unique per organization."""+ async with db_registry.async_session() as session:+ from sqlalchemy import and_, or_, select++ # Build conditions for the query+ conditions = [+ ProviderModelORM.handle == handle,+ ProviderModelORM.is_deleted == False, # Filter out soft-deleted models+ ]++ if model_type:+ conditions.append(ProviderModelORM.model_type == model_type)++ # Search for models that are either:+ # 1. Organization-specific (matching actor's org)+ # 2. Global (organization_id is NULL)+ conditions.append(or_(ProviderModelORM.organization_id == actor.organization_id, ProviderModelORM.organization_id.is_(None)))++ stmt = select(ProviderModelORM).where(and_(*conditions))+ result = await session.execute(stmt)+ models = list(result.scalars().all())++ # Find the model the user has access to+ # Prioritize org-specific models over global models+ org_model = None+ global_model = None++ for model in models:+ if model.organization_id == actor.organization_id:+ org_model = model+ elif model.organization_id is None:+ global_model = model++ # Return org-specific model if it exists, otherwise return global model+ if org_model:+ return org_model.to_pydantic()+ elif global_model:+ return global_model.to_pydantic()++ return None++ @enforce_types+ @trace_method+ async def list_models_async(+ self,+ actor: PydanticUser,+ model_type: Optional[str] = None,+ provider_id: Optional[str] = None,+ enabled: Optional[bool] = True,+ limit: Optional[int] = None,+ ) -> List[PydanticProviderModel]:+ """List models available to an actor (both global and org-scoped)."""+ async with db_registry.async_session() as session:+ # Build filters+ filters = {}+ if model_type:+ filters["model_type"] = model_type+ if provider_id:+ filters["provider_id"] = provider_id+ if enabled is not None:+ filters["enabled"] = enabled++ # Get org-scoped models (excluding soft-deleted ones)+ org_filters = {**filters, "organization_id": actor.organization_id}+ org_models = await ProviderModelORM.list_async(+ db_session=session,+ limit=limit,+ check_is_deleted=True, # Filter out soft-deleted models+ **org_filters,+ )++ # Get global models - need to handle NULL organization_id specially+ from sqlalchemy import and_, select++ # Build conditions for global models query+ conditions = [+ ProviderModelORM.organization_id.is_(None),+ ProviderModelORM.is_deleted == False, # Filter out soft-deleted models+ ]+ if model_type:+ conditions.append(ProviderModelORM.model_type == model_type)+ if provider_id:+ conditions.append(ProviderModelORM.provider_id == provider_id)+ if enabled is not None:+ conditions.append(ProviderModelORM.enabled == enabled)++ stmt = select(ProviderModelORM).where(and_(*conditions))+ if limit:+ stmt = stmt.limit(limit)+ result = await session.execute(stmt)+ global_models = list(result.scalars().all())++ # Combine and deduplicate by handle AND model_type (org-scoped takes precedence)+ # Use (handle, model_type) tuple as key since same handle can exist for LLM and embedding+ all_models = {(m.handle, m.model_type): m for m in global_models}+ all_models.update({(m.handle, m.model_type): m for m in org_models})++ return [m.to_pydantic() for m in all_models.values()]++ @enforce_types+ @trace_method+ async def get_llm_config_from_handle(+ self,+ handle: str,+ actor: PydanticUser,+ ) -> LLMConfig:+ """Get an LLMConfig from a model handle.++ Args:+ handle: The model handle to look up+ actor: The user actor for permission checking++ Returns:+ LLMConfig constructed from the provider and model data++ Raises:+ NoResultFound: If the handle doesn't exist in the database+ """+ from letta.orm.errors import NoResultFound++ # Look up the model by handle+ model = await self.get_model_by_handle_async(handle=handle, actor=actor, model_type="llm")++ if not model:+ raise NoResultFound(f"LLM model not found with handle='{handle}'")++ # Get the provider for this model+ provider = await self.get_provider_async(provider_id=model.provider_id, actor=actor)++ # Construct the LLMConfig from the model and provider data+ llm_config = LLMConfig(+ model=model.name,+ model_endpoint_type=model.model_endpoint_type,+ model_endpoint=provider.base_url or f"https://api.{provider.provider_type.value}.com/v1",+ context_window=model.max_context_window or 16384, # Default if not set+ handle=model.handle,+ provider_name=provider.name,+ provider_category=provider.provider_category,+ )++ return llm_config++ @enforce_types+ @trace_method+ async def get_embedding_config_from_handle(+ self,+ handle: str,+ actor: PydanticUser,+ ) -> EmbeddingConfig:+ """Get an EmbeddingConfig from a model handle.++ Args:+ handle: The model handle to look up+ actor: The user actor for permission checking++ Returns:+ EmbeddingConfig constructed from the provider and model data++ Raises:+ NoResultFound: If the handle doesn't exist in the database+ """+ from letta.orm.errors import NoResultFound++ # Look up the model by handle+ model = await self.get_model_by_handle_async(handle=handle, actor=actor, model_type="embedding")++ if not model:+ raise NoResultFound(f"Embedding model not found with handle='{handle}'")++ # Get the provider for this model+ provider = await self.get_provider_async(provider_id=model.provider_id, actor=actor)++ # Construct the EmbeddingConfig from the model and provider data+ embedding_config = EmbeddingConfig(+ embedding_model=model.name,+ embedding_endpoint_type=model.model_endpoint_type,+ embedding_endpoint=provider.base_url or f"https://api.{provider.provider_type.value}.com/v1",+ embedding_dim=model.embedding_dim or 1536, # Use model's dimension or default+ embedding_chunk_size=300, # Default chunk size+ handle=model.handle,+ )++ return embedding_configdiff --git a/tests/managers/test_provider_manager.py b/tests/managers/test_provider_manager.pyindex bdc3082c8..61e2597f3 100644--- a/tests/managers/test_provider_manager.py+++ b/tests/managers/test_provider_manager.py@@ -320,3 +320,182 @@ async def test_list_providers_decrypts_all(provider_manager, default_user, encry# Verify Secret getter workssecret = provider.get_api_key_secret()assert secret.get_plaintext() == f"sk-key-{i}"+++# ======================================================================================================================+# Handle to Config Conversion Tests+# ======================================================================================================================+++async def test_handle_to_llm_config_conversion(provider_manager, default_user):+ """Test that handle to LLMConfig conversion works correctly with database lookup."""+ from letta.orm.errors import NoResultFound+ from letta.schemas.embedding_config import EmbeddingConfig+ from letta.schemas.llm_config import LLMConfig++ # Create a test provider+ provider_create = ProviderCreate(+ name="test-handle-provider", provider_type=ProviderType.openai, api_key="sk-test-handle-key", base_url="https://api.openai.com/v1"+ )+ provider = await provider_manager.create_provider_async(provider_create, actor=default_user)++ # Sync some test models+ llm_models = [+ LLMConfig(+ model="gpt-4",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=8192,+ handle="test-handle-provider/gpt-4",+ provider_name=provider.name,+ provider_category=ProviderCategory.base,+ ),+ LLMConfig(+ model="gpt-3.5-turbo",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=4096,+ handle="test-handle-provider/gpt-3.5-turbo",+ provider_name=provider.name,+ provider_category=ProviderCategory.base,+ ),+ ]++ embedding_models = [+ EmbeddingConfig(+ embedding_model="text-embedding-ada-002",+ embedding_endpoint_type="openai",+ embedding_endpoint="https://api.openai.com/v1",+ embedding_dim=1536,+ embedding_chunk_size=300,+ handle="test-handle-provider/text-embedding-ada-002",+ )+ ]++ await provider_manager.sync_provider_models_async(+ provider=provider, llm_models=llm_models, embedding_models=embedding_models, organization_id=default_user.organization_id+ )++ # Test LLM config from handle+ llm_config = await provider_manager.get_llm_config_from_handle(handle="test-handle-provider/gpt-4", actor=default_user)++ # Verify the returned config+ assert llm_config.model == "gpt-4"+ assert llm_config.handle == "test-handle-provider/gpt-4"+ assert llm_config.context_window == 8192+ assert llm_config.model_endpoint == "https://api.openai.com/v1"+ assert llm_config.provider_name == "test-handle-provider"++ # Test embedding config from handle+ embedding_config = await provider_manager.get_embedding_config_from_handle(+ handle="test-handle-provider/text-embedding-ada-002", actor=default_user+ )++ # Verify the returned config+ assert embedding_config.embedding_model == "text-embedding-ada-002"+ assert embedding_config.handle == "test-handle-provider/text-embedding-ada-002"+ assert embedding_config.embedding_dim == 1536+ assert embedding_config.embedding_chunk_size == 300+ assert embedding_config.embedding_endpoint == "https://api.openai.com/v1"++ # Test context window limit override would be done at server level+ # The provider_manager method doesn't support context_window_limit directly++ # Test error handling for non-existent handle+ with pytest.raises(NoResultFound):+ await provider_manager.get_llm_config_from_handle(handle="nonexistent/model", actor=default_user)+++async def test_byok_provider_auto_syncs_models(provider_manager, default_user, monkeypatch):+ """Test that creating a BYOK provider attempts to sync its models."""+ from letta.schemas.embedding_config import EmbeddingConfig+ from letta.schemas.llm_config import LLMConfig++ # Mock the list_llm_models_async method+ async def mock_list_llm():+ return [+ LLMConfig(+ model="gpt-4o",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=128000,+ handle="openai/gpt-4o",+ provider_name="openai",+ provider_category=ProviderCategory.base,+ ),+ LLMConfig(+ model="gpt-4",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=8192,+ handle="openai/gpt-4",+ provider_name="openai",+ provider_category=ProviderCategory.base,+ ),+ ]++ # Mock the list_embedding_models_async method+ async def mock_list_embedding():+ return [+ EmbeddingConfig(+ embedding_model="text-embedding-ada-002",+ embedding_endpoint_type="openai",+ embedding_endpoint="https://api.openai.com/v1",+ embedding_dim=1536,+ embedding_chunk_size=300,+ handle="openai/text-embedding-ada-002",+ )+ ]++ # Mock the _sync_default_models_for_provider method directly+ async def mock_sync(provider, actor):+ # Get mock models and update them for this provider+ llm_models = await mock_list_llm()+ embedding_models = await mock_list_embedding()++ # Update models to match the BYOK provider+ for model in llm_models:+ model.provider_name = provider.name+ model.handle = f"{provider.name}/{model.model}"+ model.provider_category = provider.provider_category++ for model in embedding_models:+ model.handle = f"{provider.name}/{model.embedding_model}"++ # Call sync_provider_models_async with mock data+ await provider_manager.sync_provider_models_async(+ provider=provider, llm_models=llm_models, embedding_models=embedding_models, organization_id=actor.organization_id+ )++ monkeypatch.setattr(provider_manager, "_sync_default_models_for_provider", mock_sync)++ # Create a BYOK OpenAI provider (simulates UI "Add API Key" flow)+ provider_create = ProviderCreate(name="my-openai-key", provider_type=ProviderType.openai, api_key="sk-my-personal-key-123")++ # Create the BYOK provider (is_byok=True is the default)+ provider = await provider_manager.create_provider_async(provider_create, actor=default_user, is_byok=True)++ # Verify provider was created+ assert provider.name == "my-openai-key"+ assert provider.provider_type == ProviderType.openai++ # List models for this provider - they should have been auto-synced+ models = await provider_manager.list_models_async(actor=default_user, provider_id=provider.id)++ # Should have both LLM and embedding models+ llm_models = [m for m in models if m.model_type == "llm"]+ embedding_models = [m for m in models if m.model_type == "embedding"]++ assert len(llm_models) > 0, "No LLM models were synced"+ assert len(embedding_models) > 0, "No embedding models were synced"++ # Verify handles are correctly formatted with BYOK provider name+ for model in models:+ assert model.handle.startswith(f"{provider.name}/")++ # Test that we can get LLM config from handle+ llm_config = await provider_manager.get_llm_config_from_handle(handle="my-openai-key/gpt-4o", actor=default_user)+ assert llm_config.model == "gpt-4o"+ assert llm_config.provider_name == "my-openai-key"diff --git a/tests/test_agent_serialization_v2.py b/tests/test_agent_serialization_v2.pyindex a50ba3aba..04bcb11ed 100644--- a/tests/test_agent_serialization_v2.py+++ b/tests/test_agent_serialization_v2.py@@ -1147,7 +1147,7 @@ class TestAgentFileImport:"""Test basic agent import functionality with embedding override."""agent_file = await agent_serialization_manager.export([test_agent.id], default_user)- embedding_config_override = await server.get_cached_embedding_config_async(actor=other_user, handle=embedding_handle_override)+ embedding_config_override = await server.get_embedding_config_from_handle_async(actor=other_user, handle=embedding_handle_override)result = await agent_serialization_manager.import_file(agent_file, other_user, override_embedding_config=embedding_config_override)assert result.successdiff --git a/tests/test_server_providers.py b/tests/test_server_providers.pynew file mode 100644index 000000000..2306c22f1--- /dev/null+++ b/tests/test_server_providers.py@@ -0,0 +1,1742 @@+"""Tests for provider initialization via ProviderManager.sync_base_providers and provider model persistence."""++import uuid+from unittest.mock import AsyncMock, MagicMock, patch++import pytest++from letta.orm.errors import UniqueConstraintViolationError+from letta.schemas.embedding_config import EmbeddingConfig+from letta.schemas.enums import ProviderCategory, ProviderType+from letta.schemas.llm_config import LLMConfig+from letta.schemas.providers import LettaProvider, OpenAIProvider, ProviderCreate+from letta.services.organization_manager import OrganizationManager+from letta.services.provider_manager import ProviderManager+from letta.services.user_manager import UserManager+++def unique_provider_name(base_name="test-provider"):+ """Generate a unique provider name for testing."""+ return f"{base_name}-{uuid.uuid4().hex[:8]}"+++def generate_test_id():+ """Generate a unique test ID for handles and names."""+ return uuid.uuid4().hex[:8]+++async def default_organization():+ """Fixture to create and return the default organization."""+ manager = OrganizationManager()+ org = await manager.create_default_organization_async()+ yield org+++async def default_user(default_organization):+ """Fixture to create and return the default user within the default organization."""+ manager = UserManager()+ user = await manager.create_default_actor_async(org_id=default_organization.id)+ yield user+++async def provider_manager():+ """Fixture to create and return a ProviderManager instance."""+ return ProviderManager()+++async def org_manager():+ """Fixture to create and return an OrganizationManager instance."""+ return OrganizationManager()+++async def test_sync_base_providers_creates_new_providers(default_user, provider_manager):+ """Test that sync_base_providers creates providers that don't exist."""+ # Mock base providers from environment+ base_providers = [+ LettaProvider(name="letta"),+ OpenAIProvider(name="openai", api_key="sk-test-key"),+ ]++ # Sync providers to DB+ await provider_manager.sync_base_providers(base_providers=base_providers, actor=default_user)++ # Verify providers were created in the database+ letta_providers = await provider_manager.list_providers_async(name="letta", actor=default_user)+ openai_providers = await provider_manager.list_providers_async(name="openai", actor=default_user)++ assert len(letta_providers) == 1+ assert letta_providers[0].name == "letta"+ assert letta_providers[0].provider_type == ProviderType.letta++ assert len(openai_providers) == 1+ assert openai_providers[0].name == "openai"+ assert openai_providers[0].provider_type == ProviderType.openai+++async def test_sync_base_providers_skips_existing_providers(default_user, provider_manager):+ """Test that sync_base_providers skips providers that already exist."""+ # Mock base providers from environment+ base_providers = [+ LettaProvider(name="letta"),+ ]++ # Sync providers to DB first time+ await provider_manager.sync_base_providers(base_providers=base_providers, actor=default_user)++ # Sync again - should skip existing+ await provider_manager.sync_base_providers(base_providers=base_providers, actor=default_user)++ # Verify only one provider exists (not duplicated)+ letta_providers = await provider_manager.list_providers_async(name="letta", actor=default_user)+ assert len(letta_providers) == 1+++async def test_sync_base_providers_handles_race_condition(default_user, provider_manager):+ """Test that sync_base_providers handles race conditions gracefully."""+ # Mock base providers from environment+ base_providers = [+ LettaProvider(name="letta"),+ ]++ # Mock a race condition: list returns empty, but create fails with UniqueConstraintViolation+ original_list = provider_manager.list_providers_async+ original_create = provider_manager.create_provider_async++ call_count = {"count": 0}++ async def mock_list(*args, **kwargs):+ # First call returns empty (simulating race condition window)+ if call_count["count"] == 0:+ call_count["count"] += 1+ return []+ # Subsequent calls use original behavior+ return await original_list(*args, **kwargs)++ async def mock_create(*args, **kwargs):+ # Simulate another pod creating the provider first+ raise UniqueConstraintViolationError("Provider already exists")++ with patch.object(provider_manager, "list_providers_async", side_effect=mock_list):+ with patch.object(provider_manager, "create_provider_async", side_effect=mock_create):+ # This should NOT raise an exception+ await provider_manager.sync_base_providers(base_providers=base_providers, actor=default_user)+++async def test_sync_base_providers_handles_none_api_key(default_user, provider_manager):+ """Test that sync_base_providers handles providers with None api_key."""+ # Mock base providers from environment (Letta doesn't need an API key)+ base_providers = [+ LettaProvider(name="letta", api_key=None),+ ]++ # Sync providers to DB - should convert None to empty string+ await provider_manager.sync_base_providers(base_providers=base_providers, actor=default_user)++ # Verify provider was created+ letta_providers = await provider_manager.list_providers_async(name="letta", actor=default_user)+ assert len(letta_providers) == 1+ assert letta_providers[0].name == "letta"+++async def test_sync_provider_models_async(default_user, provider_manager):+ """Test that sync_provider_models_async persists LLM and embedding models to database."""+ # First create a provider in the database+ test_id = generate_test_id()+ provider_create = ProviderCreate(+ name=f"test-openai-{test_id}",+ provider_type=ProviderType.openai,+ api_key="sk-test-key",+ )+ provider = await provider_manager.create_provider_async(provider_create, actor=default_user, is_byok=False)++ # Mock LLM and embedding models with unique handles+ llm_models = [+ LLMConfig(+ model=f"gpt-4o-mini-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=16384,+ handle=f"test-{test_id}/gpt-4o-mini",+ provider_name=provider.name,+ provider_category=ProviderCategory.base,+ ),+ LLMConfig(+ model=f"gpt-4o-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=128000,+ handle=f"test-{test_id}/gpt-4o",+ provider_name=provider.name,+ provider_category=ProviderCategory.base,+ ),+ ]++ embedding_models = [+ EmbeddingConfig(+ embedding_model=f"text-embedding-3-small-{test_id}",+ embedding_endpoint_type="openai",+ embedding_endpoint="https://api.openai.com/v1",+ embedding_dim=1536, # Add required embedding_dim+ embedding_chunk_size=300,+ handle=f"test-{test_id}/text-embedding-3-small",+ ),+ ]++ # Sync models to database+ await provider_manager.sync_provider_models_async(+ provider=provider,+ llm_models=llm_models,+ embedding_models=embedding_models,+ organization_id=None, # Global models+ )++ # Verify models were persisted+ llm_model = await provider_manager.get_model_by_handle_async(+ handle=f"test-{test_id}/gpt-4o-mini",+ actor=default_user,+ model_type="llm",+ )++ assert llm_model is not None+ assert llm_model.handle == f"test-{test_id}/gpt-4o-mini"+ assert llm_model.name == f"gpt-4o-mini-{test_id}"+ assert llm_model.model_type == "llm"+ assert llm_model.provider_id == provider.id+ assert llm_model.organization_id is None # Global model+ assert llm_model.max_context_window == 16384+ assert llm_model.supports_token_streaming == True++ embedding_model = await provider_manager.get_model_by_handle_async(+ handle=f"test-{test_id}/text-embedding-3-small",+ actor=default_user,+ model_type="embedding",+ )++ assert embedding_model is not None+ assert embedding_model.handle == f"test-{test_id}/text-embedding-3-small"+ assert embedding_model.name == f"text-embedding-3-small-{test_id}"+ assert embedding_model.model_type == "embedding"+++async def test_sync_provider_models_idempotent(default_user, provider_manager):+ """Test that sync_provider_models_async is idempotent and doesn't duplicate models."""+ # First create a provider in the database+ test_id = uuid.uuid4().hex[:8]+ provider_create = ProviderCreate(+ name=f"test-openai-{test_id}",+ provider_type=ProviderType.openai,+ api_key="sk-test-key",+ )+ provider = await provider_manager.create_provider_async(provider_create, actor=default_user, is_byok=False)++ # Mock LLM models with unique handle+ llm_models = [+ LLMConfig(+ model=f"gpt-4o-mini-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=16384,+ handle=f"test-{test_id}/gpt-4o-mini",+ provider_name=provider.name,+ provider_category=ProviderCategory.base,+ ),+ ]++ # Sync models to database twice+ await provider_manager.sync_provider_models_async(+ provider=provider,+ llm_models=llm_models,+ embedding_models=[],+ organization_id=None,+ )++ await provider_manager.sync_provider_models_async(+ provider=provider,+ llm_models=llm_models,+ embedding_models=[],+ organization_id=None,+ )++ # Verify only one model exists+ models = await provider_manager.list_models_async(+ actor=default_user,+ model_type="llm",+ provider_id=provider.id,+ )++ # Filter for our specific model+ test_handle = f"test-{test_id}/gpt-4o-mini"+ gpt_models = [m for m in models if m.handle == test_handle]+ assert len(gpt_models) == 1+++async def test_get_model_by_handle_async_org_scoped(default_user, provider_manager):+ """Test that get_model_by_handle_async returns both base and BYOK providers/models."""+ test_id = generate_test_id()++ # Create a base provider+ base_provider_create = ProviderCreate(+ name=f"test-base-openai-{test_id}",+ provider_type=ProviderType.openai,+ api_key="sk-test-key",+ )+ base_provider = await provider_manager.create_provider_async(base_provider_create, actor=default_user, is_byok=False)++ # Create a BYOK provider with same type+ byok_provider_create = ProviderCreate(+ name=f"test-byok-openai-{test_id}",+ provider_type=ProviderType.openai,+ api_key="sk-byok-key",+ )+ byok_provider = await provider_manager.create_provider_async(byok_provider_create, actor=default_user, is_byok=True)++ # Create global base models with unique handles+ global_base_model = LLMConfig(+ model=f"gpt-4o-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=128000,+ handle=f"test-{test_id}/base-gpt-4o", # Unique handle for base model+ provider_name=base_provider.name,+ provider_category=ProviderCategory.base,+ )++ global_base_model_2 = LLMConfig(+ model=f"gpt-3.5-turbo-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=4096,+ handle=f"test-{test_id}/base-gpt-3.5-turbo", # Unique handle+ provider_name=base_provider.name,+ provider_category=ProviderCategory.base,+ )++ await provider_manager.sync_provider_models_async(+ provider=base_provider,+ llm_models=[global_base_model, global_base_model_2],+ embedding_models=[],+ organization_id=None, # Global+ )++ # Create org-scoped BYOK models with different unique handles+ org_byok_model = LLMConfig(+ model=f"gpt-4o-custom-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://custom.openai.com/v1",+ context_window=64000,+ handle=f"test-{test_id}/byok-gpt-4o", # Different unique handle for BYOK+ provider_name=byok_provider.name,+ provider_category=ProviderCategory.byok,+ )++ org_byok_model_2 = LLMConfig(+ model=f"gpt-4o-mini-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://custom.openai.com/v1",+ context_window=16384,+ handle=f"test-{test_id}/byok-gpt-4o-mini", # Unique handle+ provider_name=byok_provider.name,+ provider_category=ProviderCategory.byok,+ )++ # Sync all BYOK models at once+ await provider_manager.sync_provider_models_async(+ provider=byok_provider,+ llm_models=[org_byok_model, org_byok_model_2],+ embedding_models=[],+ organization_id=default_user.organization_id, # Org-scoped+ )++ # Test 1: Get base model by its unique handle+ model = await provider_manager.get_model_by_handle_async(+ handle=f"test-{test_id}/base-gpt-4o",+ actor=default_user,+ model_type="llm",+ )++ assert model is not None+ assert model.organization_id is None # Global base model+ assert model.max_context_window == 128000+ assert model.provider_id == base_provider.id++ # Test 2: Get BYOK model by its unique handle+ model_2 = await provider_manager.get_model_by_handle_async(+ handle=f"test-{test_id}/byok-gpt-4o",+ actor=default_user,+ model_type="llm",+ )++ assert model_2 is not None+ assert model_2.organization_id == default_user.organization_id # Org-scoped BYOK+ assert model_2.max_context_window == 64000+ assert model_2.provider_id == byok_provider.id++ # Test 3: Get another BYOK model+ model_3 = await provider_manager.get_model_by_handle_async(+ handle=f"test-{test_id}/byok-gpt-4o-mini",+ actor=default_user,+ model_type="llm",+ )++ assert model_3 is not None+ assert model_3.organization_id == default_user.organization_id+ assert model_3.max_context_window == 16384+ assert model_3.provider_id == byok_provider.id++ # Test 4: Get base model+ model_4 = await provider_manager.get_model_by_handle_async(+ handle=f"test-{test_id}/base-gpt-3.5-turbo",+ actor=default_user,+ model_type="llm",+ )++ assert model_4 is not None+ assert model_4.organization_id is None # Global model+ assert model_4.max_context_window == 4096+ assert model_4.provider_id == base_provider.id++ # Test 5: List all models to verify both base and BYOK are returned+ all_models = await provider_manager.list_models_async(+ actor=default_user,+ model_type="llm",+ )++ test_handles = {m.handle for m in all_models if test_id in m.handle}+ # Should have 4 unique models with unique handles+ assert f"test-{test_id}/base-gpt-4o" in test_handles+ assert f"test-{test_id}/base-gpt-3.5-turbo" in test_handles+ assert f"test-{test_id}/byok-gpt-4o" in test_handles+ assert f"test-{test_id}/byok-gpt-4o-mini" in test_handles+++async def test_get_model_by_handle_async_unique_handles(default_user, provider_manager):+ """Test that handles are unique within each organization scope."""+ test_id = generate_test_id()++ # Create a base provider+ provider_create = ProviderCreate(+ name=f"test-openai-{test_id}",+ provider_type=ProviderType.openai,+ api_key="sk-test-key",+ )+ provider = await provider_manager.create_provider_async(provider_create, actor=default_user, is_byok=False)++ # Create a global model with a unique handle+ test_handle = f"test-{test_id}/gpt-4o"+ global_model = LLMConfig(+ model=f"gpt-4o-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=128000,+ handle=test_handle,+ provider_name=provider.name,+ provider_category=ProviderCategory.base,+ )++ await provider_manager.sync_provider_models_async(+ provider=provider,+ llm_models=[global_model],+ embedding_models=[],+ organization_id=None, # Global+ )++ # Test 1: Verify the global model was created+ model = await provider_manager.get_model_by_handle_async(+ handle=test_handle,+ actor=default_user,+ model_type="llm",+ )++ assert model is not None+ assert model.organization_id is None # Global model+ assert model.max_context_window == 128000++ # Test 2: Create an org-scoped model with the SAME handle - should work now (different org scope)+ org_model_same_handle = LLMConfig(+ model=f"gpt-4o-custom-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://custom.openai.com/v1",+ context_window=64000,+ handle=test_handle, # Same handle - allowed since different org+ provider_name=provider.name,+ provider_category=ProviderCategory.byok,+ )++ # This should work now since handles are unique per org, not globally+ await provider_manager.sync_provider_models_async(+ provider=provider,+ llm_models=[org_model_same_handle],+ embedding_models=[],+ organization_id=default_user.organization_id, # Org-scoped+ )++ # Verify we now get the org-specific model (prioritized over global)+ model_check = await provider_manager.get_model_by_handle_async(+ handle=test_handle,+ actor=default_user,+ model_type="llm",+ )++ # Should now return the org-specific model (prioritized over global)+ assert model_check is not None+ assert model_check.organization_id == default_user.organization_id # Org-specific+ assert model_check.max_context_window == 64000 # Org model's context window++ # Test 3: Create a model with a different unique handle - should succeed+ different_handle = f"test-{test_id}/gpt-4o-mini"+ org_model = LLMConfig(+ model=f"gpt-4o-mini-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://custom.openai.com/v1",+ context_window=16384,+ handle=different_handle, # Different handle+ provider_name=provider.name,+ provider_category=ProviderCategory.byok,+ )++ await provider_manager.sync_provider_models_async(+ provider=provider,+ llm_models=[org_model],+ embedding_models=[],+ organization_id=default_user.organization_id, # Org-scoped+ )++ # Verify the org model was created+ org_model_result = await provider_manager.get_model_by_handle_async(+ handle=different_handle,+ actor=default_user,+ model_type="llm",+ )++ assert org_model_result is not None+ assert org_model_result.organization_id == default_user.organization_id+ assert org_model_result.max_context_window == 16384++ # Test 4: Get model with handle that doesn't exist - should return None+ nonexistent_model = await provider_manager.get_model_by_handle_async(+ handle=f"test-{test_id}/nonexistent",+ actor=default_user,+ model_type="llm",+ )++ assert nonexistent_model is None+++async def test_list_models_async_combines_global_and_org(default_user, provider_manager):+ """Test that list_models_async returns both global and org-scoped models with org-scoped taking precedence."""+ # Create a provider in the database with unique test ID+ test_id = generate_test_id()+ provider_create = ProviderCreate(+ name=f"test-openai-{test_id}",+ provider_type=ProviderType.openai,+ api_key="sk-test-key",+ )+ provider = await provider_manager.create_provider_async(provider_create, actor=default_user, is_byok=False)++ # Create global models with unique handles+ global_models = [+ LLMConfig(+ model=f"gpt-4o-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=128000,+ handle=f"test-{test_id}/gpt-4o",+ provider_name=provider.name,+ provider_category=ProviderCategory.base,+ ),+ LLMConfig(+ model=f"gpt-4o-mini-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=16384,+ handle=f"test-{test_id}/gpt-4o-mini",+ provider_name=provider.name,+ provider_category=ProviderCategory.base,+ ),+ ]++ await provider_manager.sync_provider_models_async(+ provider=provider,+ llm_models=global_models,+ embedding_models=[],+ organization_id=None, # Global+ )++ # Create org-scoped model with a different unique handle+ org_model = LLMConfig(+ model=f"gpt-4o-custom-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://custom.openai.com/v1",+ context_window=64000,+ handle=f"test-{test_id}/gpt-4o-custom", # Different unique handle+ provider_name=provider.name,+ provider_category=ProviderCategory.byok,+ )++ await provider_manager.sync_provider_models_async(+ provider=provider,+ llm_models=[org_model],+ embedding_models=[],+ organization_id=default_user.organization_id, # Org-scoped+ )++ # List models+ models = await provider_manager.list_models_async(+ actor=default_user,+ model_type="llm",+ provider_id=provider.id,+ )++ # Should have 3 unique models+ handles = {m.handle for m in models}+ assert f"test-{test_id}/gpt-4o" in handles+ assert f"test-{test_id}/gpt-4o-mini" in handles+ assert f"test-{test_id}/gpt-4o-custom" in handles++ # gpt-4o should be the global version+ gpt4o = next(m for m in models if m.handle == f"test-{test_id}/gpt-4o")+ assert gpt4o.organization_id is None+ assert gpt4o.max_context_window == 128000++ # gpt-4o-mini should be the global version+ gpt4o_mini = next(m for m in models if m.handle == f"test-{test_id}/gpt-4o-mini")+ assert gpt4o_mini.organization_id is None+ assert gpt4o_mini.max_context_window == 16384++ # gpt-4o-custom should be the org-scoped version+ gpt4o_custom = next(m for m in models if m.handle == f"test-{test_id}/gpt-4o-custom")+ assert gpt4o_custom.organization_id == default_user.organization_id+ assert gpt4o_custom.max_context_window == 64000+++async def test_list_models_async_filters(default_user, provider_manager):+ """Test that list_models_async properly applies filters."""+ # Create providers in the database with unique test ID+ test_id = generate_test_id()+ openai_create = ProviderCreate(+ name=f"test-openai-{test_id}",+ provider_type=ProviderType.openai,+ api_key="sk-test-key",+ )+ openai_provider = await provider_manager.create_provider_async(openai_create, actor=default_user, is_byok=False)++ # For anthropic, we need to use a valid provider type+ anthropic_create = ProviderCreate(+ name=f"test-anthropic-{test_id}",+ provider_type=ProviderType.anthropic,+ api_key="sk-test-key",+ )+ anthropic_provider = await provider_manager.create_provider_async(anthropic_create, actor=default_user, is_byok=False)++ # Create models for different providers with unique handles+ openai_llm = LLMConfig(+ model=f"gpt-4o-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=128000,+ handle=f"test-{test_id}/openai-gpt-4o",+ provider_name=openai_provider.name,+ provider_category=ProviderCategory.base,+ )++ openai_embedding = EmbeddingConfig(+ embedding_model=f"text-embedding-3-small-{test_id}",+ embedding_endpoint_type="openai",+ embedding_endpoint="https://api.openai.com/v1",+ embedding_dim=1536, # Add required embedding_dim+ embedding_chunk_size=300,+ handle=f"test-{test_id}/openai-text-embedding",+ )++ anthropic_llm = LLMConfig(+ model=f"claude-3-5-sonnet-{test_id}",+ model_endpoint_type="anthropic",+ model_endpoint="https://api.anthropic.com",+ context_window=200000,+ handle=f"test-{test_id}/anthropic-claude",+ provider_name=anthropic_provider.name,+ provider_category=ProviderCategory.base,+ )++ await provider_manager.sync_provider_models_async(+ provider=openai_provider,+ llm_models=[openai_llm],+ embedding_models=[openai_embedding],+ organization_id=None,+ )++ await provider_manager.sync_provider_models_async(+ provider=anthropic_provider,+ llm_models=[anthropic_llm],+ embedding_models=[],+ organization_id=None,+ )++ # Test filter by model_type+ llm_models = await provider_manager.list_models_async(+ actor=default_user,+ model_type="llm",+ )+ llm_handles = {m.handle for m in llm_models}+ assert f"test-{test_id}/openai-gpt-4o" in llm_handles+ assert f"test-{test_id}/anthropic-claude" in llm_handles+ assert f"test-{test_id}/openai-text-embedding" not in llm_handles++ embedding_models = await provider_manager.list_models_async(+ actor=default_user,+ model_type="embedding",+ )+ embedding_handles = {m.handle for m in embedding_models}+ assert f"test-{test_id}/openai-text-embedding" in embedding_handles+ assert f"test-{test_id}/openai-gpt-4o" not in embedding_handles+ assert f"test-{test_id}/anthropic-claude" not in embedding_handles++ # Test filter by provider_id+ openai_models = await provider_manager.list_models_async(+ actor=default_user,+ provider_id=openai_provider.id,+ )+ openai_handles = {m.handle for m in openai_models}+ assert f"test-{test_id}/openai-gpt-4o" in openai_handles+ assert f"test-{test_id}/openai-text-embedding" in openai_handles+ assert f"test-{test_id}/anthropic-claude" not in openai_handles+++async def test_model_metadata_persistence(default_user, provider_manager):+ """Test that model metadata like context window, streaming, and tool calling are properly persisted."""+ # Create a provider in the database+ test_id = generate_test_id()+ provider_create = ProviderCreate(+ name=f"test-openai-{test_id}",+ provider_type=ProviderType.openai,+ api_key="sk-test-key",+ )+ provider = await provider_manager.create_provider_async(provider_create, actor=default_user, is_byok=False)++ # Create model with specific metadata and unique handle+ llm_model = LLMConfig(+ model=f"gpt-4o-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=128000,+ handle=f"test-{test_id}/gpt-4o",+ provider_name=provider.name,+ provider_category=ProviderCategory.base,+ )++ await provider_manager.sync_provider_models_async(+ provider=provider,+ llm_models=[llm_model],+ embedding_models=[],+ organization_id=None,+ )++ # Retrieve model and verify metadata+ model = await provider_manager.get_model_by_handle_async(+ handle=f"test-{test_id}/gpt-4o",+ actor=default_user,+ model_type="llm",+ )++ assert model is not None+ assert model.max_context_window == 128000+ assert model.supports_token_streaming == True # OpenAI supports streaming+ assert model.supports_tool_calling == True # Assumed true for LLMs+ assert model.model_endpoint_type == "openai"+ assert model.enabled == True+++async def test_model_enabled_filter(default_user, provider_manager):+ """Test that enabled filter works properly in list_models_async."""+ # Create a provider in the database+ provider_create = ProviderCreate(+ name=unique_provider_name("test-openai"),+ provider_type=ProviderType.openai,+ api_key="sk-test-key",+ )+ provider = await provider_manager.create_provider_async(provider_create, actor=default_user, is_byok=False)++ # Create models+ models = [+ LLMConfig(+ model="gpt-4o",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=128000,+ handle="openai/gpt-4o",+ provider_name="openai",+ provider_category=ProviderCategory.base,+ ),+ LLMConfig(+ model="gpt-4o-mini",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=16384,+ handle="openai/gpt-4o-mini",+ provider_name="openai",+ provider_category=ProviderCategory.base,+ ),+ ]++ await provider_manager.sync_provider_models_async(+ provider=provider,+ llm_models=models,+ embedding_models=[],+ organization_id=None,+ )++ # All models should be enabled by default+ enabled_models = await provider_manager.list_models_async(+ actor=default_user,+ enabled=True,+ )++ handles = {m.handle for m in enabled_models}+ assert "openai/gpt-4o" in handles+ assert "openai/gpt-4o-mini" in handles++ # Test with enabled=None (should return all models)+ all_models = await provider_manager.list_models_async(+ actor=default_user,+ enabled=None,+ )++ all_handles = {m.handle for m in all_models}+ assert "openai/gpt-4o" in all_handles+ assert "openai/gpt-4o-mini" in all_handles+++async def test_get_llm_config_from_handle_uses_cached_models(default_user):+ """Test that get_llm_config_from_handle_async uses cached models from database instead of querying provider."""+ from letta.server.server import SyncServer++ server = SyncServer(init_with_default_org_and_user=False)+ server.default_user = default_user++ # Create a provider and model in database+ provider = OpenAIProvider(name="openai", api_key="sk-test-key")+ provider.id = "provider_test_id"+ provider.provider_category = ProviderCategory.base+ provider.base_url = "https://custom.openai.com/v1"++ # Mock the provider manager methods+ server.provider_manager = AsyncMock()++ # Mock get_llm_config_from_handle to return cached LLM config+ mock_llm_config = LLMConfig(+ model="gpt-4o",+ model_endpoint_type="openai",+ model_endpoint="https://custom.openai.com/v1",+ context_window=128000,+ handle="openai/gpt-4o",+ provider_name="openai",+ provider_category=ProviderCategory.base,+ )+ server.provider_manager.get_llm_config_from_handle.return_value = mock_llm_config++ # Get LLM config - should use cached data+ llm_config = await server.get_llm_config_from_handle_async(+ actor=default_user,+ handle="openai/gpt-4o",+ context_window_limit=100000,+ )++ # Verify it used the cached model data+ assert llm_config.model == "gpt-4o"+ assert llm_config.model_endpoint == "https://custom.openai.com/v1"+ assert llm_config.context_window == 100000 # Limited by context_window_limit+ assert llm_config.handle == "openai/gpt-4o"+ assert llm_config.provider_name == "openai"++ # Verify provider methods were called+ server.provider_manager.get_llm_config_from_handle.assert_called_once_with(+ handle="openai/gpt-4o",+ actor=default_user,+ )+++async def test_get_embedding_config_from_handle_uses_cached_models(default_user):+ """Test that get_embedding_config_from_handle_async uses cached models from database instead of querying provider."""+ from letta.server.server import SyncServer++ server = SyncServer(init_with_default_org_and_user=False)+ server.default_user = default_user++ # Mock the provider manager methods+ server.provider_manager = AsyncMock()++ # Mock get_embedding_config_from_handle to return cached embedding config+ mock_embedding_config = EmbeddingConfig(+ embedding_model="text-embedding-3-small",+ embedding_endpoint_type="openai",+ embedding_endpoint="https://custom.openai.com/v1",+ embedding_dim=1536,+ embedding_chunk_size=500,+ handle="openai/text-embedding-3-small",+ )+ server.provider_manager.get_embedding_config_from_handle.return_value = mock_embedding_config++ # Get embedding config - should use cached data+ embedding_config = await server.get_embedding_config_from_handle_async(+ actor=default_user,+ handle="openai/text-embedding-3-small",+ embedding_chunk_size=500,+ )++ # Verify it used the cached model data+ assert embedding_config.embedding_model == "text-embedding-3-small"+ assert embedding_config.embedding_endpoint == "https://custom.openai.com/v1"+ assert embedding_config.embedding_chunk_size == 500+ assert embedding_config.handle == "openai/text-embedding-3-small"+ # Note: EmbeddingConfig doesn't have provider_name field unlike LLMConfig++ # Verify provider methods were called+ server.provider_manager.get_embedding_config_from_handle.assert_called_once_with(+ handle="openai/text-embedding-3-small",+ actor=default_user,+ )+++async def test_server_sync_provider_models_on_init(default_user):+ """Test that the server syncs provider models to database during initialization."""+ from letta.server.server import SyncServer++ server = SyncServer(init_with_default_org_and_user=False)+ server.default_user = default_user++ # Mock providers+ mock_letta_provider = AsyncMock()+ mock_letta_provider.name = "letta"+ mock_letta_provider.list_llm_models_async.return_value = [+ LLMConfig(+ model="letta-model",+ model_endpoint_type="openai", # Use valid endpoint type+ model_endpoint="https://api.letta.com",+ context_window=8192,+ handle="letta/letta-model",+ provider_name="letta",+ provider_category=ProviderCategory.base,+ )+ ]+ mock_letta_provider.list_embedding_models_async.return_value = []++ mock_openai_provider = AsyncMock()+ mock_openai_provider.name = "openai"+ mock_openai_provider.list_llm_models_async.return_value = [+ LLMConfig(+ model="gpt-4o",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=128000,+ handle="openai/gpt-4o",+ provider_name="openai",+ provider_category=ProviderCategory.base,+ )+ ]+ mock_openai_provider.list_embedding_models_async.return_value = [+ EmbeddingConfig(+ embedding_model="text-embedding-3-small",+ embedding_endpoint_type="openai",+ embedding_endpoint="https://api.openai.com/v1",+ embedding_dim=1536, # Add required embedding_dim+ embedding_chunk_size=300,+ handle="openai/text-embedding-3-small",+ )+ ]++ server._enabled_providers = [mock_letta_provider, mock_openai_provider]++ # Mock provider manager+ server.provider_manager = AsyncMock()++ # Mock list_providers_async to return providers with IDs+ db_letta = MagicMock()+ db_letta.id = "letta_provider_id"+ db_letta.name = "letta"++ db_openai = MagicMock()+ db_openai.id = "openai_provider_id"+ db_openai.name = "openai"++ server.provider_manager.list_providers_async.return_value = [db_letta, db_openai]++ # Call the sync method+ await server._sync_provider_models_async()++ # Verify models were synced for each provider+ assert server.provider_manager.sync_provider_models_async.call_count == 2++ # Verify Letta models were synced+ letta_call = server.provider_manager.sync_provider_models_async.call_args_list[0]+ assert letta_call.kwargs["provider"].id == "letta_provider_id"+ assert len(letta_call.kwargs["llm_models"]) == 1+ assert len(letta_call.kwargs["embedding_models"]) == 0+ assert letta_call.kwargs["organization_id"] is None++ # Verify OpenAI models were synced+ openai_call = server.provider_manager.sync_provider_models_async.call_args_list[1]+ assert openai_call.kwargs["provider"].id == "openai_provider_id"+ assert len(openai_call.kwargs["llm_models"]) == 1+ assert len(openai_call.kwargs["embedding_models"]) == 1+ assert openai_call.kwargs["organization_id"] is None+++async def test_provider_model_unique_constraint_per_org(default_user, provider_manager, org_manager, default_organization):+ """Test that provider models have unique handles within each organization (not globally)."""+ # Create a second organization+ from letta.schemas.organization import Organization++ org2 = Organization(name="Test Org 2")+ org2 = await org_manager.create_organization_async(org2)++ # Create a user for the second organization+ from letta.services.user_manager import UserManager++ user_manager = UserManager()+ # Note: create_default_actor_async has a bug where it ignores the org_id parameter+ # Create a user properly for org2+ from letta.schemas.user import User++ org2_user = User(name="Test User Org2", organization_id=org2.id)+ org2_user = await user_manager.create_actor_async(org2_user)++ # Create a global base provider+ provider_create = ProviderCreate(+ name=unique_provider_name("test-openai"),+ provider_type=ProviderType.openai,+ api_key="sk-test-key",+ )+ provider = await provider_manager.create_provider_async(provider_create, actor=default_user, is_byok=False)++ # Create model configuration with a unique handle for this test+ import uuid++ test_id = uuid.uuid4().hex[:8]+ test_handle = f"test-{test_id}/gpt-4o"+ model_org1 = LLMConfig(+ model=f"gpt-4o-org1-{test_id}", # Unique model name per org+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=128000,+ handle=test_handle,+ provider_name=provider.name,+ provider_category=ProviderCategory.base,+ )++ # Sync for default organization+ await provider_manager.sync_provider_models_async(+ provider=provider,+ llm_models=[model_org1],+ embedding_models=[],+ organization_id=default_organization.id,+ )++ # Create model with same handle but different model name for org2+ model_org2 = LLMConfig(+ model=f"gpt-4o-org2-{test_id}", # Different model name for org2+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=128000,+ handle=test_handle, # Same handle - now allowed since handles are unique per org+ provider_name=provider.name,+ provider_category=ProviderCategory.base,+ )++ # Sync for organization 2 with same handle - now allowed since handles are unique per org+ await provider_manager.sync_provider_models_async(+ provider=provider,+ llm_models=[model_org2],+ embedding_models=[],+ organization_id=org2.id,+ )++ # Each organization should have its own model with the same handle+ org1_model = await provider_manager.get_model_by_handle_async(+ handle=test_handle,+ actor=default_user,+ model_type="llm",+ )++ org2_model = await provider_manager.get_model_by_handle_async(+ handle=test_handle,+ actor=org2_user,+ model_type="llm",+ )++ # Both organizations should have their own models with the same handle+ assert org1_model is not None, "Model should exist for org1"+ assert org2_model is not None, "Model should exist for org2"++ # Each model should belong to its respective organization+ assert org1_model.organization_id == default_organization.id+ assert org2_model.organization_id == org2.id++ # They should have the same handle but different IDs+ assert org1_model.handle == org2_model.handle == test_handle+ assert org1_model.id != org2_model.id++ # Now create a model with a different handle for org2+ test_handle_org2 = f"test-{test_id}/gpt-4o-org2"+ model_org2 = LLMConfig(+ model="gpt-4o",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=128000,+ handle=test_handle_org2, # Different handle+ provider_name=provider.name,+ provider_category=ProviderCategory.base,+ )++ # Sync for organization 2 with different handle+ await provider_manager.sync_provider_models_async(+ provider=provider,+ llm_models=[model_org2],+ embedding_models=[],+ organization_id=org2.id,+ )++ # Now org2 should see their model+ org2_model_new = await provider_manager.get_model_by_handle_async(+ handle=test_handle_org2,+ actor=org2_user,+ model_type="llm",+ )++ assert org2_model_new is not None+ assert org2_model_new.handle == test_handle_org2+ assert org2_model_new.organization_id == org2.id+++async def test_sync_provider_models_add_remove_models(default_user, provider_manager):+ """+ Test that sync_provider_models_async correctly handles:+ 1. Adding new models to an existing provider+ 2. Removing models from an existing provider+ 3. Not dropping non-base (BYOK) provider models during sync+ """+ # Create a base provider+ test_id = generate_test_id()+ provider_create = ProviderCreate(+ name=f"test-openai-{test_id}",+ provider_type=ProviderType.openai,+ api_key="sk-test-key",+ )+ base_provider = await provider_manager.create_provider_async(provider_create, actor=default_user, is_byok=False)++ # Create a BYOK provider with same provider type+ byok_provider_create = ProviderCreate(+ name=f"test-openai-byok-{test_id}",+ provider_type=ProviderType.openai,+ api_key="sk-byok-key",+ )+ byok_provider = await provider_manager.create_provider_async(byok_provider_create, actor=default_user, is_byok=True)++ # Initial sync: Create initial base models+ initial_base_models = [+ LLMConfig(+ model=f"gpt-4o-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=128000,+ handle=f"test-{test_id}/gpt-4o",+ provider_name=base_provider.name,+ provider_category=ProviderCategory.base,+ ),+ LLMConfig(+ model=f"gpt-4o-mini-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=16384,+ handle=f"test-{test_id}/gpt-4o-mini",+ provider_name=base_provider.name,+ provider_category=ProviderCategory.base,+ ),+ ]++ await provider_manager.sync_provider_models_async(+ provider=base_provider,+ llm_models=initial_base_models,+ embedding_models=[],+ organization_id=None, # Global base models+ )++ # Create BYOK models (should not be affected by base provider sync)+ byok_models = [+ LLMConfig(+ model=f"custom-gpt-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://custom.api.com/v1",+ context_window=64000,+ handle=f"test-{test_id}/custom-gpt",+ provider_name=byok_provider.name,+ provider_category=ProviderCategory.byok,+ ),+ ]++ await provider_manager.sync_provider_models_async(+ provider=byok_provider,+ llm_models=byok_models,+ embedding_models=[],+ organization_id=default_user.organization_id, # Org-scoped BYOK+ )++ # Verify initial state: all 3 models exist+ all_models = await provider_manager.list_models_async(+ actor=default_user,+ model_type="llm",+ )+ handles = {m.handle for m in all_models}+ assert f"test-{test_id}/gpt-4o" in handles+ assert f"test-{test_id}/gpt-4o-mini" in handles+ assert f"test-{test_id}/custom-gpt" in handles++ # Second sync: Add a new model and remove one existing model+ updated_base_models = [+ # Keep gpt-4o+ LLMConfig(+ model=f"gpt-4o-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=128000,+ handle=f"test-{test_id}/gpt-4o",+ provider_name=base_provider.name,+ provider_category=ProviderCategory.base,+ ),+ # Remove gpt-4o-mini (not in this list)+ # Add new model gpt-4-turbo+ LLMConfig(+ model=f"gpt-4-turbo-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=128000,+ handle=f"test-{test_id}/gpt-4-turbo",+ provider_name=base_provider.name,+ provider_category=ProviderCategory.base,+ ),+ ]++ await provider_manager.sync_provider_models_async(+ provider=base_provider,+ llm_models=updated_base_models,+ embedding_models=[],+ organization_id=None, # Global base models+ )++ # Verify updated state+ all_models_after = await provider_manager.list_models_async(+ actor=default_user,+ model_type="llm",+ )+ handles_after = {m.handle for m in all_models_after}++ # gpt-4o should still exist (kept)+ assert f"test-{test_id}/gpt-4o" in handles_after++ # gpt-4o-mini should be removed+ assert f"test-{test_id}/gpt-4o-mini" not in handles_after++ # gpt-4-turbo should be added+ assert f"test-{test_id}/gpt-4-turbo" in handles_after++ # BYOK model should NOT be affected by base provider sync+ assert f"test-{test_id}/custom-gpt" in handles_after++ # Verify the BYOK model still belongs to the correct provider+ byok_model = await provider_manager.get_model_by_handle_async(+ handle=f"test-{test_id}/custom-gpt",+ actor=default_user,+ model_type="llm",+ )+ assert byok_model is not None+ assert byok_model.provider_id == byok_provider.id+ assert byok_model.organization_id == default_user.organization_id++ # Third sync: Remove all base provider models+ await provider_manager.sync_provider_models_async(+ provider=base_provider,+ llm_models=[], # Empty list - remove all models+ embedding_models=[],+ organization_id=None,+ )++ # Verify all base models are removed+ all_models_final = await provider_manager.list_models_async(+ actor=default_user,+ model_type="llm",+ )+ handles_final = {m.handle for m in all_models_final}++ # All base provider models should be gone+ assert f"test-{test_id}/gpt-4o" not in handles_final+ assert f"test-{test_id}/gpt-4-turbo" not in handles_final++ # But BYOK model should still exist+ assert f"test-{test_id}/custom-gpt" in handles_final+++async def test_sync_provider_models_mixed_llm_and_embedding(default_user, provider_manager):+ """+ Test that sync_provider_models_async correctly handles adding/removing both LLM and embedding models,+ ensuring that changes to one model type don't affect the other.+ """+ test_id = generate_test_id()+ provider_create = ProviderCreate(+ name=f"test-openai-{test_id}",+ provider_type=ProviderType.openai,+ api_key="sk-test-key",+ )+ provider = await provider_manager.create_provider_async(provider_create, actor=default_user, is_byok=False)++ # Initial sync: LLM and embedding models+ initial_llm_models = [+ LLMConfig(+ model=f"gpt-4o-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=128000,+ handle=f"test-{test_id}/gpt-4o",+ provider_name=provider.name,+ provider_category=ProviderCategory.base,+ ),+ ]++ initial_embedding_models = [+ EmbeddingConfig(+ embedding_model=f"text-embedding-3-small-{test_id}",+ embedding_endpoint_type="openai",+ embedding_endpoint="https://api.openai.com/v1",+ embedding_dim=1536,+ embedding_chunk_size=300,+ handle=f"test-{test_id}/text-embedding-3-small",+ ),+ ]++ await provider_manager.sync_provider_models_async(+ provider=provider,+ llm_models=initial_llm_models,+ embedding_models=initial_embedding_models,+ organization_id=None,+ )++ # Verify initial state+ llm_models = await provider_manager.list_models_async(+ actor=default_user,+ model_type="llm",+ provider_id=provider.id,+ )+ embedding_models = await provider_manager.list_models_async(+ actor=default_user,+ model_type="embedding",+ provider_id=provider.id,+ )+ assert len([m for m in llm_models if m.handle == f"test-{test_id}/gpt-4o"]) == 1+ assert len([m for m in embedding_models if m.handle == f"test-{test_id}/text-embedding-3-small"]) == 1++ # Second sync: Add new LLM, remove embedding+ updated_llm_models = [+ # Keep existing+ LLMConfig(+ model=f"gpt-4o-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=128000,+ handle=f"test-{test_id}/gpt-4o",+ provider_name=provider.name,+ provider_category=ProviderCategory.base,+ ),+ # Add new+ LLMConfig(+ model=f"gpt-4o-mini-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=16384,+ handle=f"test-{test_id}/gpt-4o-mini",+ provider_name=provider.name,+ provider_category=ProviderCategory.base,+ ),+ ]++ await provider_manager.sync_provider_models_async(+ provider=provider,+ llm_models=updated_llm_models,+ embedding_models=[], # Remove all embeddings+ organization_id=None,+ )++ # Verify updated state+ llm_models_after = await provider_manager.list_models_async(+ actor=default_user,+ model_type="llm",+ provider_id=provider.id,+ )+ embedding_models_after = await provider_manager.list_models_async(+ actor=default_user,+ model_type="embedding",+ provider_id=provider.id,+ )++ llm_handles = {m.handle for m in llm_models_after}+ embedding_handles = {m.handle for m in embedding_models_after}++ # Both LLM models should exist+ assert f"test-{test_id}/gpt-4o" in llm_handles+ assert f"test-{test_id}/gpt-4o-mini" in llm_handles++ # Embedding should be removed+ assert f"test-{test_id}/text-embedding-3-small" not in embedding_handles+++async def test_provider_name_uniqueness_within_org(default_user, provider_manager):+ """Test that provider names must be unique within an organization, including conflicts with base provider names."""+ test_id = generate_test_id()++ # Create a base provider with a specific name+ base_provider_name = f"test-provider-{test_id}"+ base_provider_create = ProviderCreate(+ name=base_provider_name,+ provider_type=ProviderType.openai,+ api_key="sk-test-key",+ )+ await provider_manager.create_provider_async(base_provider_create, actor=default_user, is_byok=False)++ # Test 1: Attempt to create another base provider with the same name - should fail with ValueError+ with pytest.raises(ValueError, match="already exists"):+ duplicate_provider_create = ProviderCreate(+ name=base_provider_name, # Same name+ provider_type=ProviderType.anthropic, # Different type+ api_key="sk-different-key",+ )+ await provider_manager.create_provider_async(duplicate_provider_create, actor=default_user, is_byok=False)++ # Test 2: Create a BYOK provider with the same name as a base provider - should fail with ValueError+ with pytest.raises(ValueError, match="conflicts with an existing base provider"):+ byok_duplicate_create = ProviderCreate(+ name=base_provider_name, # Same name as base provider+ provider_type=ProviderType.openai,+ api_key="sk-byok-key",+ )+ await provider_manager.create_provider_async(byok_duplicate_create, actor=default_user, is_byok=True)++ # Test 3: Create a provider with a different name - should succeed+ different_provider_create = ProviderCreate(+ name=f"different-provider-{test_id}",+ provider_type=ProviderType.openai,+ api_key="sk-another-key",+ )+ different_provider = await provider_manager.create_provider_async(different_provider_create, actor=default_user, is_byok=False)+ assert different_provider is not None+ assert different_provider.name == f"different-provider-{test_id}"+++async def test_model_name_uniqueness_within_provider(default_user, provider_manager):+ """Test that model names must be unique within a provider."""+ test_id = generate_test_id()++ # Create a provider+ provider_create = ProviderCreate(+ name=f"test-provider-{test_id}",+ provider_type=ProviderType.openai,+ api_key="sk-test-key",+ )+ provider = await provider_manager.create_provider_async(provider_create, actor=default_user, is_byok=False)++ # Create initial models with unique names+ initial_models = [+ LLMConfig(+ model=f"model-1-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=4096,+ handle=f"test-{test_id}/model-1",+ provider_name=provider.name,+ provider_category=ProviderCategory.base,+ ),+ LLMConfig(+ model=f"model-2-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=8192,+ handle=f"test-{test_id}/model-2",+ provider_name=provider.name,+ provider_category=ProviderCategory.base,+ ),+ ]++ await provider_manager.sync_provider_models_async(+ provider=provider,+ llm_models=initial_models,+ embedding_models=[],+ organization_id=None,+ )++ # Test 1: Try to sync models with duplicate names within the same provider - should be idempotent+ duplicate_models = [+ LLMConfig(+ model=f"model-1-{test_id}", # Same model name+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=4096,+ handle=f"test-{test_id}/model-1", # Same handle+ provider_name=provider.name,+ provider_category=ProviderCategory.base,+ ),+ LLMConfig(+ model=f"model-1-{test_id}", # Duplicate model name in same sync+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=16384, # Different settings+ handle=f"test-{test_id}/model-1-duplicate", # Different handle+ provider_name=provider.name,+ provider_category=ProviderCategory.base,+ ),+ ]++ # This should raise an error or handle the duplication appropriately+ # The behavior depends on the implementation - it might dedupe or raise an error+ try:+ await provider_manager.sync_provider_models_async(+ provider=provider,+ llm_models=duplicate_models,+ embedding_models=[],+ organization_id=None,+ )+ # If it doesn't raise an error, verify that we don't have duplicate models+ all_models = await provider_manager.list_models_async(+ actor=default_user,+ model_type="llm",+ provider_id=provider.id,+ )++ # Count how many times each model name appears+ model_names = [m.name for m in all_models if test_id in m.name]+ model_1_count = model_names.count(f"model-1-{test_id}")++ # Should only have one model with this name per provider+ assert model_1_count <= 2, f"Found {model_1_count} models with name 'model-1-{test_id}', expected at most 2"++ except (UniqueConstraintViolationError, ValueError):+ # This is also acceptable behavior - raising an error for duplicate model names+ pass++ # Test 2: Different providers can have models with the same name+ provider_2_create = ProviderCreate(+ name=f"test-provider-2-{test_id}",+ provider_type=ProviderType.openai,+ api_key="sk-test-key-2",+ )+ provider_2 = await provider_manager.create_provider_async(provider_2_create, actor=default_user, is_byok=False)++ # Create a model with the same name but in a different provider - should succeed+ same_name_different_provider = [+ LLMConfig(+ model=f"model-1-{test_id}", # Same model name as in provider 1+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=4096,+ handle=f"test-{test_id}/provider2-model-1", # Different handle+ provider_name=provider_2.name,+ provider_category=ProviderCategory.base,+ ),+ ]++ await provider_manager.sync_provider_models_async(+ provider=provider_2,+ llm_models=same_name_different_provider,+ embedding_models=[],+ organization_id=None,+ )++ # Verify the model was created+ provider_2_models = await provider_manager.list_models_async(+ actor=default_user,+ model_type="llm",+ provider_id=provider_2.id,+ )++ assert any(m.name == f"model-1-{test_id}" for m in provider_2_models)+++async def test_handle_uniqueness_per_org(default_user, provider_manager):+ """Test that handles must be unique within organizations but can be duplicated across different orgs."""+ test_id = generate_test_id()++ # Create providers+ provider_1_create = ProviderCreate(+ name=f"test-provider-1-{test_id}",+ provider_type=ProviderType.openai,+ api_key="sk-test-key",+ )+ provider_1 = await provider_manager.create_provider_async(provider_1_create, actor=default_user, is_byok=False)++ provider_2_create = ProviderCreate(+ name=f"test-provider-2-{test_id}",+ provider_type=ProviderType.anthropic,+ api_key="sk-test-key-2",+ )+ provider_2 = await provider_manager.create_provider_async(provider_2_create, actor=default_user, is_byok=False)++ # Create a global base model with a specific handle+ base_handle = f"test-{test_id}/unique-handle"+ base_model = LLMConfig(+ model=f"base-model-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=4096,+ handle=base_handle,+ provider_name=provider_1.name,+ provider_category=ProviderCategory.base,+ )++ await provider_manager.sync_provider_models_async(+ provider=provider_1,+ llm_models=[base_model],+ embedding_models=[],+ organization_id=None, # Global+ )++ # Test 1: Try to create another global model with the same handle from different provider+ # This should succeed because we need a different model name (provider constraint)+ duplicate_handle_model = LLMConfig(+ model=f"different-model-{test_id}", # Different model name (required for provider uniqueness)+ model_endpoint_type="anthropic",+ model_endpoint="https://api.anthropic.com",+ context_window=8192,+ handle=base_handle, # Same handle - allowed since different model name+ provider_name=provider_2.name,+ provider_category=ProviderCategory.base,+ )++ # This will create another global model with same handle but different provider/model name+ await provider_manager.sync_provider_models_async(+ provider=provider_2,+ llm_models=[duplicate_handle_model],+ embedding_models=[],+ organization_id=None, # Global+ )++ # The get_model_by_handle_async will return one of the global models+ model = await provider_manager.get_model_by_handle_async(+ handle=base_handle,+ actor=default_user,+ model_type="llm",+ )++ # Should return one of the global models+ assert model is not None+ assert model.organization_id is None # Global model++ # Test 2: Org-scoped model CAN have the same handle as a global model+ org_model_same_handle = LLMConfig(+ model=f"org-model-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://custom.openai.com/v1",+ context_window=16384,+ handle=base_handle, # Same handle as global model - now allowed for different org+ provider_name=provider_1.name,+ provider_category=ProviderCategory.byok,+ )++ # This should succeed - handles are unique per org, not globally+ await provider_manager.sync_provider_models_async(+ provider=provider_1,+ llm_models=[org_model_same_handle],+ embedding_models=[],+ organization_id=default_user.organization_id, # Org-scoped+ )++ # When user from this org queries, they should get their org-specific model (prioritized)+ model = await provider_manager.get_model_by_handle_async(+ handle=base_handle,+ actor=default_user,+ model_type="llm",+ )++ assert model is not None+ assert model.organization_id == default_user.organization_id # Org-specific model (prioritized)+ assert model.max_context_window == 16384 # Org model's context window++ # Test 3: Create a model with a new unique handle - should succeed+ unique_org_handle = f"test-{test_id}/org-unique-handle"++ org_model_1 = LLMConfig(+ model=f"org-model-1-{test_id}",+ model_endpoint_type="openai",+ model_endpoint="https://api.openai.com/v1",+ context_window=8192,+ handle=unique_org_handle,+ provider_name=provider_1.name,+ provider_category=ProviderCategory.byok,+ )++ await provider_manager.sync_provider_models_async(+ provider=provider_1,+ llm_models=[org_model_1],+ embedding_models=[],+ organization_id=default_user.organization_id,+ )++ # Verify the model was created+ model = await provider_manager.get_model_by_handle_async(+ handle=unique_org_handle,+ actor=default_user,+ model_type="llm",+ )++ assert model is not None+ assert model.organization_id == default_user.organization_id+ assert model.max_context_window == 8192++ # Test 4: Try to create another model with the same handle even in different org - NOT allowed+ org_model_2 = LLMConfig(+ model=f"org-model-2-{test_id}",+ model_endpoint_type="anthropic",+ model_endpoint="https://api.anthropic.com",+ context_window=16384,+ handle=unique_org_handle, # Same handle - globally unique+ provider_name=provider_2.name,+ provider_category=ProviderCategory.byok,+ )++ # This should be idempotent+ await provider_manager.sync_provider_models_async(+ provider=provider_2,+ llm_models=[org_model_2],+ embedding_models=[],+ organization_id=default_user.organization_id, # Same or different org doesn't matter+ )++ # Verify still the original model+ model = await provider_manager.get_model_by_handle_async(+ handle=unique_org_handle,+ actor=default_user,+ model_type="llm",+ )++ assert model is not None+ assert model.provider_id == provider_1.id # Still original provider+ assert model.max_context_window == 8192 # Still original