Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Bentoml BentoML Depends Function

From Leeroopedia

Overview

The bentoml.depends() function is the primary API for wiring BentoML services together in a dependency graph. It returns a Dependency[T] descriptor that resolves at runtime to either an in-process proxy or an inter-process RPC client, depending on the deployment mode.

Code Reference

Import

import bentoml

# Used as:
dependency = bentoml.depends(SomeService)

Function Signature

def depends(
    on: Service[T] | None = None,
    *,
    url: str | None = None,
    deployment: str | None = None,
    cluster: str | None = None,
) -> Dependency[T]

Source Location

src/_bentoml_sdk/service/dependency.py:L135-167

Parameters

Parameter Type Default Description
on None None The local service class to depend on. This is the most common usage for composing services defined in the same project.
url None None A remote URL pointing to an already-deployed BentoML service. Use this to depend on services running externally.
deployment None None The name of a BentoCloud deployment. The framework resolves this to the deployment's URL at runtime.
cluster None None The BentoCloud cluster name, used in conjunction with deployment to identify the target deployment.

Inputs and Outputs

Inputs: One of the following (mutually exclusive):

  • A local Service class (via the on parameter or as positional argument)
  • A remote URL string (via url)
  • A BentoCloud deployment reference (via deployment and optionally cluster)

Outputs: A Dependency[T] descriptor object with the following behavior:

  • The descriptor's __get__ method returns a proxy that mirrors the dependent service's API
  • The proxy supports .to_async for converting synchronous calls to async coroutines
  • All dependencies are collected in the parent service's dependencies dict via __set_name__
  • The get() method resolves the dependency for inter-process communication

Usage Examples

Local Service Dependency

import bentoml

@bentoml.service(resources={"gpu": 1})
class ModelService:
    @bentoml.api
    def predict(self, data: list[float]) -> list[float]:
        return self.model(data)

@bentoml.service
class Gateway:
    model = bentoml.depends(ModelService)

    @bentoml.api
    def infer(self, data: list[float]) -> list[float]:
        return self.model.predict(data)

Remote URL Dependency

import bentoml

@bentoml.service
class Gateway:
    external_model = bentoml.depends(url="https://my-model.example.com")

    @bentoml.api
    def infer(self, data: list[float]) -> dict:
        return self.external_model.predict(data)

BentoCloud Deployment Dependency

import bentoml

@bentoml.service
class Gateway:
    cloud_model = bentoml.depends(deployment="my-inference-service", cluster="production")

    @bentoml.api
    def infer(self, data: list[float]) -> dict:
        return self.cloud_model.predict(data)

Internal Mechanism

The Dependency class (defined at src/_bentoml_sdk/service/dependency.py:L48-102) implements the Python descriptor protocol:

class Dependency(t.Generic[T]):
    def __set_name__(self, owner: type, name: str) -> None:
        # Registers the dependency in the owner service's dependencies dict
        ...

    def __get__(self, obj: Any, owner: type) -> T:
        # Returns a proxy to the resolved service
        # In-process: returns the service instance directly
        # Inter-process: returns an RPC client proxy
        ...

    def get(self) -> T:
        # Resolves the dependency for inter-process communication
        ...

Relationship to Principle

This function implements the Service Dependency Injection principle by providing a declarative, framework-managed mechanism for wiring services together.

Principle:Bentoml_BentoML_Service_Dependency_Injection

Metadata

Knowledge Sources

2026-02-13 15:00 GMT

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment