Engine 模块

Engine 模块涵盖 AgentScope Runtime 的核心执行、部署与服务能力,以下 API 参考与 agentscope_runtime.engine 目录结构保持一致,便于按模块查阅。

子模块

App

class agentscope_runtime.engine.app.base_app.BaseApp(broker_url=None, backend_url=None)[source]

Bases: CeleryMixin

BaseApp integrates Celery for asynchronous background task execution, and provides FastAPI-like routing for task endpoints.

Parameters:
  • broker_url (str | None)

  • backend_url (str | None)

__init__(broker_url=None, backend_url=None)[source]
Parameters:
  • broker_url (str | None)

  • backend_url (str | None)

task(path, queue='celery')[source]

Register an asynchronous task endpoint. POST <path> -> Create a task and return task ID GET <path>/{task_id} -> Check the task status and result Combines Celery and FastAPI routing functionality.

Parameters:
class agentscope_runtime.engine.app.agent_app.AgentApp(*, app_name='', app_description='', endpoint_path='/process', response_type='sse', stream=True, request_model=<class 'agentscope_runtime.engine.schemas.agent_schemas.AgentRequest'>, before_start=None, after_finish=None, broker_url=None, backend_url=None, runner=None, enable_embedded_worker=False, **kwargs)[source]

Bases: BaseApp

The AgentApp class represents an application that runs as an agent.

Parameters:
  • app_name (str)

  • app_description (str)

  • endpoint_path (str)

  • response_type (str)

  • stream (bool)

  • request_model (type[BaseModel] | None)

  • before_start (Callable | None)

  • after_finish (Callable | None)

  • broker_url (str | None)

  • backend_url (str | None)

  • runner (Runner | None)

  • enable_embedded_worker (bool)

__init__(*, app_name='', app_description='', endpoint_path='/process', response_type='sse', stream=True, request_model=<class 'agentscope_runtime.engine.schemas.agent_schemas.AgentRequest'>, before_start=None, after_finish=None, broker_url=None, backend_url=None, runner=None, enable_embedded_worker=False, **kwargs)[source]

Initialize the AgentApp.

Parameters:
  • *args – Variable length argument list.

  • **kwargs – Arbitrary keyword arguments.

  • app_name (str)

  • app_description (str)

  • endpoint_path (str)

  • response_type (str)

  • stream (bool)

  • request_model (type[BaseModel] | None)

  • before_start (Callable | None)

  • after_finish (Callable | None)

  • broker_url (str | None)

  • backend_url (str | None)

  • runner (Runner | None)

  • enable_embedded_worker (bool)

init(func)[source]

Register init hook (support async and sync functions).

query(framework='agentscope')[source]

Register run hook and optionally specify agent framework. Allowed framework values: ‘agentscope’, ‘autogen’, ‘agno’, ‘langgraph’.

Parameters:

framework (str | None)

shutdown(func)[source]

Register shutdown hook (support async and sync functions).

run(host='0.0.0.0', port=8080, web_ui=False, **kwargs)[source]

Launch the AgentApp HTTP API server.

This method starts a FastAPI server for the agent service. Optionally, it can also launch a browser-based Web UI for interacting with the agent.

Note: If web_ui=True and this is the first time launching the Web UI, additional time may be required to initialize. This happens because the underlying Node.js command (npx @agentscope-ai/chat agentscope-runtime-webui) might install dependencies and set up the runtime environment.

Parameters:
  • host (str) – Host address to bind to. Default “0.0.0.0”.

  • port (int) – Port number to serve the application on. Default 8080.

  • web_ui (bool) – If True, launches the Agentscope Web UI in a separate process, pointing it to the API endpoint. This allows interactive use via browser. Default False.

  • **kwargs – Additional keyword arguments passed to FastAPIAppFactory when creating the FastAPI application.

Example

>>> app = AgentApp(app_name="MyAgent")
>>> app.chat(host="127.0.0.1", port=8080, web_ui=True)
get_fastapi_app(**kwargs)[source]

Get the FastAPI application

async deploy(deployer, **kwargs)[source]

Deploy the agent app with custom endpoints support

Parameters:

deployer (DeployManager)

endpoint(path, methods=None)[source]

Decorator to register custom endpoints

Parameters:
task(path, queue='default')[source]

Decorator to register custom task endpoints

Parameters:
class agentscope_runtime.engine.app.celery_mixin.CeleryMixin(broker_url=None, backend_url=None)[source]

Bases: object

Celery task processing mixin that provides core Celery functionality. Can be reused by BaseApp and FastAPIAppFactory.

Parameters:
  • broker_url (str | None)

  • backend_url (str | None)

__init__(broker_url=None, backend_url=None)[source]
Parameters:
  • broker_url (str | None)

  • backend_url (str | None)

get_registered_queues()[source]
Return type:

set[str]

register_celery_task(func, queue='celery')[source]

Register a Celery task for the given function.

Parameters:
run_task_processor(loglevel='INFO', concurrency=None, queues=None)[source]

Run Celery worker in this process.

Parameters:
  • loglevel (str)

  • concurrency (int | None)

  • queues (List[str] | None)

get_task_status(task_id)[source]

Get task status from Celery result backend.

Parameters:

task_id (str)

submit_task(func, *args, **kwargs)[source]

Submit task directly to Celery queue.

Parameters:

func (Callable)

Runner

class agentscope_runtime.engine.runner.Runner[source]

Bases: object

__init__()[source]

Initializes a runner as core instance.

Return type:

None

async query_handler(*args, **kwargs)[source]

Handle agent query.

async init_handler(*args, **kwargs)[source]

Init handler.

async shutdown_handler(*args, **kwargs)[source]

Shutdown handler.

async start()[source]
async stop()[source]
async __aenter__()[source]

Initializes the runner

Return type:

Runner

async deploy(deploy_manager=<agentscope_runtime.engine.deployers.local_deployer.LocalDeployManager object>, endpoint_path='/process', stream=True, protocol_adapters=None, requirements=None, extra_packages=None, base_image='python:3.9-slim', environment=None, runtime_config=None, **kwargs)[source]

Deploys the agent as a service.

Parameters:
  • deploy_manager (DeployManager) – Deployment manager to handle service deployment

  • endpoint_path (str) – API endpoint path for the processing function

  • stream (bool) – If start a streaming service

  • protocol_adapters (list[ProtocolAdapter] | None) – protocol adapters

  • requirements (str | None | List[str]) – PyPI dependencies

  • extra_packages (List[str] | None) – User code directory/file path

  • base_image (str) – Docker base image (for containerized deployment)

  • environment (Dict[str, str] | None) – Environment variables dict

  • runtime_config (Dict | None) – Runtime configuration dict

  • **kwargs – Additional arguments passed to deployment manager

Returns:

URL of the deployed service

Raises:

RuntimeError – If deployment fails

async stream_query(request, **kwargs)[source]

Streams the agent.

Parameters:
  • request (AgentRequest | dict)

  • kwargs (Any)

Return type:

AsyncGenerator[Event, None]

Helpers

Agent Protocol Data Generator

Provides layered Builder pattern tools to generate streaming response data that conforms to types/agent definitions

class agentscope_runtime.engine.helpers.agent_api_builder.ContentBuilder(message_builder, content_type='text', index=0)[source]

Bases: object

Content Builder

Responsible for building and managing individual Content objects, supporting Text, Image, and Data content types

Parameters:
  • message_builder (MessageBuilder)

  • content_type (str)

  • index (int)

__init__(message_builder, content_type='text', index=0)[source]

Initialize Content Builder

Parameters:
  • message_builder (MessageBuilder) – Associated MessageBuilder object

  • content_type (str) – Content type (‘text’, ‘image’, ‘data’)

  • index (int) – Content index, defaults to 0

add_text_delta(text)[source]

Add text delta (only applicable to text type)

Parameters:

text (str) – Text fragment

Returns:

Delta content object

Return type:

TextContent

set_text(text)[source]

Set complete text content (only applicable to text type)

Parameters:

text (str) – Complete text content

Returns:

Content object

Return type:

TextContent

set_refusal(text)[source]

Set complete refusal content (only applicable to refusal type)

Parameters:

text (str) – Complete refusal content

Returns:

Content object

Return type:

RefusalContent

set_image_url(image_url)[source]

Set image URL (only applicable to image type)

Parameters:

image_url (str) – Image URL

Returns:

Content object

Return type:

ImageContent

set_data(data)[source]

Set data content (only applicable to data type)

Parameters:

data (Dict[str, Any]) – Data dictionary

Returns:

Content object

Return type:

DataContent

update_data(key, value)[source]

Update specific fields of data content (only applicable to data type)

Parameters:
  • key (str) – Data key

  • value (Any) – Data value

Returns:

Content object

Return type:

DataContent

add_data_delta(delta_data)[source]

Add data delta (only applicable to data type)

Parameters:

delta_data (Dict[str, Any]) – Delta data dictionary

Returns:

Delta content object

Return type:

DataContent

add_delta(text)[source]

Add text delta (backward compatibility method)

Parameters:

text (str) – Text fragment

Returns:

Delta content object

Return type:

TextContent

complete()[source]

Complete content building

Returns:

Dictionary representation of complete Content object

Return type:

Message

get_content_data()[source]

Get dictionary representation of current content

Returns:

Content object

Return type:

Content

class agentscope_runtime.engine.helpers.agent_api_builder.MessageBuilder(response_builder, role='assistant')[source]

Bases: object

Message Builder

Responsible for building and managing individual Message objects and updating associated Response

Parameters:
  • response_builder (ResponseBuilder)

  • role (str)

__init__(response_builder, role='assistant')[source]

Initialize Message Builder

Parameters:
  • response_builder (ResponseBuilder) – Associated ResponseBuilder object

  • role (str) – Message role, defaults to assistant

create_content_builder(content_type='text')[source]

Create Content Builder

Parameters:

content_type (str) – Content type (‘text’, ‘image’, ‘data’)

Returns:

Newly created ContentBuilder instance

Return type:

ContentBuilder

add_content(content)[source]

Add content to message

Parameters:

content (Content) – Content object

get_message_data()[source]

Get dictionary representation of current message

Returns:

Message object

Return type:

Message

complete()[source]

Complete message building

Returns:

Dictionary representation of complete message object

Return type:

Message

class agentscope_runtime.engine.helpers.agent_api_builder.ResponseBuilder(session_id=None, response_id=None)[source]

Bases: object

Response Builder

Responsible for building and managing AgentResponse objects, coordinating MessageBuilder work

Parameters:
  • session_id (str | None)

  • response_id (str | None)

__init__(session_id=None, response_id=None)[source]

Initialize Response Builder

Parameters:
  • session_id (str | None) – Session ID, optional

  • response_id (str | None)

reset()[source]

Reset builder state, generate new ID and object instances

get_response_data()[source]

Get dictionary representation of current response

Returns:

Response object

Return type:

AgentResponse

created()[source]

Set response status to created

Returns:

Response object

Return type:

AgentResponse

in_progress()[source]

Set response status to in_progress

Returns:

Response object

Return type:

AgentResponse

completed()[source]

Set response status to completed

Returns:

Response object

Return type:

AgentResponse

create_message_builder(role='assistant', message_type='message')[source]

Create Message Builder

Parameters:
  • role (str) – Message role, defaults to assistant

  • message_type (str) – Message type, defaults to message

Returns:

Newly created MessageBuilder instance

Return type:

MessageBuilder

add_message(message)[source]

Add message to response output list

Parameters:

message (Message) – Message object

update_message(message)[source]

Update message in response

Parameters:

message (Message) – Updated Message object

generate_streaming_response(text_tokens, role='assistant')[source]

Generate complete streaming response sequence

Parameters:
  • text_tokens (List[str]) – Text fragment list

  • role (str) – Message role, defaults to assistant

Yields:

Dictionary of response objects generated in order

Return type:

Generator[Dict[str, Any], None, None]

agentscope_runtime.engine.helpers.agent_api_builder.StreamingResponseBuilder

alias of ResponseBuilder

class agentscope_runtime.engine.helpers.runner.SimpleRunner[source]

Bases: Runner

__init__()[source]

Initializes a runner as core instance.

Return type:

None

async query_handler(request=None, **kwargs)[source]

Handle agent query.

Parameters:

request (AgentRequest | None)

class agentscope_runtime.engine.helpers.runner.ErrorRunner[source]

Bases: Runner

__init__()[source]

Initializes a runner as core instance.

Return type:

None

async query_handler(request=None, **kwargs)[source]

Handle agent query.

Parameters:

request (AgentRequest | None)

常量

Deployers

class agentscope_runtime.engine.deployers.base.DeployManager(state_manager=None)[source]

Bases: ABC

__init__(state_manager=None)[source]
abstract async deploy(*args, **kwargs)[source]

Deploy the service and return a dictionary with deploy_id and URL.

Return type:

Dict[str, str]

abstract async stop(deploy_id, **kwargs)[source]

Stop the deployed service.

Parameters:
  • deploy_id (str) – Deployment identifier

  • **kwargs – Platform-specific parameters (url, namespace, etc.)

Returns:

  • success (bool): Whether stop succeeded

  • message (str): Status message

  • details (dict, optional): Platform-specific details

Return type:

Dict with keys

class agentscope_runtime.engine.deployers.local_deployer.LocalDeployManager(host='127.0.0.1', port=8090, shutdown_timeout=30, startup_timeout=30, logger=None)[source]

Bases: DeployManager

Unified LocalDeployManager supporting multiple deployment modes.

Parameters:
  • host (str)

  • port (int)

  • shutdown_timeout (int)

  • startup_timeout (int)

  • logger (Logger | None)

__init__(host='127.0.0.1', port=8090, shutdown_timeout=30, startup_timeout=30, logger=None)[source]

Initialize LocalDeployManager.

Parameters:
  • host (str) – Host to bind to

  • port (int) – Port to bind to

  • shutdown_timeout (int) – Timeout for graceful shutdown

  • logger (Logger | None) – Logger instance

  • startup_timeout (int)

async deploy(app=None, runner=None, endpoint_path='/process', request_model=None, response_type='sse', stream=True, before_start=None, after_finish=None, mode=DeploymentMode.DAEMON_THREAD, custom_endpoints=None, protocol_adapters=None, broker_url=None, backend_url=None, enable_embedded_worker=False, project_dir=None, entrypoint=None, **kwargs)[source]

Deploy using unified FastAPI architecture.

Parameters:
  • app – Agent app to be deployed

  • runner – Runner instance (for DAEMON_THREAD mode)

  • endpoint_path (str) – API endpoint path

  • request_model (Type | None) – Pydantic model for request validation

  • response_type (str) – Response type - “json”, “sse”, or “text”

  • stream (bool) – Enable streaming responses

  • before_start (Callable | None) – Callback function called before server starts

  • after_finish (Callable | None) – Callback function called after server finishes

  • mode (DeploymentMode) – Deployment mode

  • custom_endpoints (List[Dict] | None) – Custom endpoints from agent app

  • protocol_adapters (list[ProtocolAdapter] | None) – Protocol adapters

  • broker_url (str | None) – Celery broker URL for background task processing

  • backend_url (str | None) – Celery backend URL for result storage

  • enable_embedded_worker (bool) – Whether to run Celery worker embedded in the app

  • project_dir (str | None) – Project directory (for DETACHED_PROCESS mode)

  • entrypoint (str | None) – Entrypoint specification (for DETACHED_PROCESS mode)

  • **kwargs (Any) – Additional keyword arguments

Returns:

Dict containing deploy_id and url

Raises:

RuntimeError – If deployment fails

Return type:

Dict[str, str]

async static create_detached_project(app=None, runner=None, entrypoint=None, endpoint_path='/process', requirements=None, extra_packages=None, protocol_adapters=None, custom_endpoints=None, broker_url=None, backend_url=None, enable_embedded_worker=False, platform='local', **kwargs)[source]
Parameters:
  • runner (Any | None)

  • entrypoint (str | None)

  • endpoint_path (str)

  • requirements (str | None | List[str])

  • extra_packages (List[str] | None)

  • protocol_adapters (list[ProtocolAdapter] | None)

  • custom_endpoints (List[Dict] | None)

  • broker_url (str | None)

  • backend_url (str | None)

  • enable_embedded_worker (bool)

  • platform (str)

Return type:

str

async stop(deploy_id, **kwargs)[source]

Stop the FastAPI service.

Parameters:
  • deploy_id (str) – Deployment identifier

  • **kwargs – Additional parameters

Returns:

Dict with success status, message, and details

Return type:

Dict[str, Any]

is_service_running()[source]

Check if service is running.

Return type:

bool

get_deployment_info()[source]

Get deployment information.

Return type:

Dict[str, Any]

property service_url: str | None

Get the current service URL if running.

class agentscope_runtime.engine.deployers.kubernetes_deployer.K8sConfig(*, k8s_namespace='agentscope-runtime', kubeconfig_path=None)[source]

Bases: BaseModel

Parameters:
  • k8s_namespace (str | None)

  • kubeconfig_path (str | None)

k8s_namespace: str | None
kubeconfig_path: str | None
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.deployers.kubernetes_deployer.BuildConfig(*, build_context_dir=None, dockerfile_template=None, build_timeout=600, push_timeout=300, cleanup_after_build=True)[source]

Bases: BaseModel

Build configuration

Parameters:
  • build_context_dir (str | None)

  • dockerfile_template (str)

  • build_timeout (int)

  • push_timeout (int)

  • cleanup_after_build (bool)

build_context_dir: str | None
dockerfile_template: str
build_timeout: int
push_timeout: int
cleanup_after_build: bool
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.deployers.kubernetes_deployer.KubernetesDeployManager(kube_config=None, registry_config=RegistryConfig(registry_url='', username=None, password=None, namespace='agentscope-runtime', image_pull_secret=None), use_deployment=True, build_context_dir=None, state_manager=None)[source]

Bases: DeployManager

Kubernetes deployer for agent services

Parameters:
  • kube_config (K8sConfig)

  • registry_config (RegistryConfig)

  • use_deployment (bool)

  • build_context_dir (str | None)

__init__(kube_config=None, registry_config=RegistryConfig(registry_url='', username=None, password=None, namespace='agentscope-runtime', image_pull_secret=None), use_deployment=True, build_context_dir=None, state_manager=None)[source]
Parameters:
  • kube_config (K8sConfig | None)

  • registry_config (RegistryConfig)

  • use_deployment (bool)

  • build_context_dir (str | None)

static get_service_endpoint(service_external_ip, service_port, fallback_host='127.0.0.1')[source]

Auto-select appropriate service endpoint based on detected environment.

Solves the common issue where Kubernetes LoadBalancer/ExternalIP is not reachable from localhost in local clusters (e.g., Minikube/Kind).

Parameters:
  • service_external_ip (str | None) – ExternalIP or LoadBalancer IP from Service

  • service_port (int | list | None) – Target port

  • fallback_host (str) – Host to use in local environments (default:

  • 127.0.0.1)

Returns:

Full HTTP endpoint URL: http://<host>:<port>

Return type:

str

Example

>>> endpoint = get_service_endpoint('192.168.5.1', 8080)
>>> # In local env → 'http://127.0.0.1:8080'
>>> # In cloud env → 'http://192.168.5.1:8080'
static get_resource_name(deploy_id)[source]
Parameters:

deploy_id (str)

Return type:

str

async deploy(app=None, runner=None, entrypoint=None, endpoint_path='/process', stream=True, custom_endpoints=None, protocol_adapters=None, requirements=None, extra_packages=None, base_image='python:3.9-slim', environment=None, runtime_config=None, port=8090, replicas=1, mount_dir=None, image_name='agent_llm', image_tag='latest', push_to_registry=False, use_cache=True, **kwargs)[source]

Deploy runner to Kubernetes.

All temporary files are created in cwd/.agentscope_runtime/ by default.

Parameters:
  • app – Agent app to be deployed

  • runner – Complete Runner object with agent, environment_manager, context_manager

  • entrypoint (str | None) – Entrypoint spec (e.g., “app.py” or “app.py:handler”)

  • endpoint_path (str) – API endpoint path

  • stream (bool) – Enable streaming responses

  • custom_endpoints (List[Dict] | None) – Custom endpoints from agent app

  • protocol_adapters (list[ProtocolAdapter] | None) – protocol adapters

  • requirements (str | None | List[str]) – PyPI dependencies (following _agent_engines.py pattern)

  • extra_packages (List[str] | None) – User code directory/file path

  • base_image (str) – Docker base image

  • port (int) – Container port

  • replicas (int) – Number of replicas

  • environment (Dict | None) – Environment variables dict

  • mount_dir (str | None) – Mount directory

  • runtime_config (Dict | None) – K8s runtime configuration

  • use_cache (bool) – Enable build cache (default: True)

  • compatibility (# Backward)

  • image_name (str) – Image name

  • image_tag (str) – Image tag

  • push_to_registry (bool) – Push to registry

  • **kwargs – Additional arguments

Returns:

Dict containing deploy_id, url, resource_name, replicas

Raises:

RuntimeError – If deployment fails

Return type:

Dict[str, Any]

async stop(deploy_id, **kwargs)[source]

Stop Kubernetes deployment.

Parameters:
  • deploy_id (str) – Deployment identifier

  • **kwargs – Additional parameters

Returns:

Dict with success status, message, and details

Return type:

Dict[str, Any]

get_status()[source]

Get deployment status

Return type:

str

class agentscope_runtime.engine.deployers.modelstudio_deployer.OSSConfig(*, region='cn-hangzhou', access_key_id=None, access_key_secret=None, bucket_prefix='tmpbucket-agentscope-runtime')[source]

Bases: BaseModel

Parameters:
  • region (str)

  • access_key_id (str | None)

  • access_key_secret (str | None)

  • bucket_prefix (str)

region: str
access_key_id: str | None
access_key_secret: str | None
bucket_prefix: str
classmethod from_env()[source]
Return type:

OSSConfig

ensure_valid()[source]
Return type:

None

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.deployers.modelstudio_deployer.ModelstudioConfig(*, endpoint='bailian.cn-beijing.aliyuncs.com', workspace_id=None, access_key_id=None, access_key_secret=None, dashscope_api_key=None)[source]

Bases: BaseModel

Parameters:
  • endpoint (str)

  • workspace_id (str | None)

  • access_key_id (str | None)

  • access_key_secret (str | None)

  • dashscope_api_key (str | None)

endpoint: str
workspace_id: str | None
access_key_id: str | None
access_key_secret: str | None
dashscope_api_key: str | None
classmethod from_env()[source]
Return type:

ModelstudioConfig

ensure_valid()[source]
Return type:

None

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.deployers.modelstudio_deployer.ModelstudioDeployManager(oss_config=None, modelstudio_config=None, build_root=None, state_manager=None)[source]

Bases: DeployManager

Deployer for Alibaba Modelstudio Function Compute based agent deployment.

This deployer packages the user project into a wheel, uploads it to OSS, and triggers a Modelstudio Full-Code deploy.

Parameters:
  • oss_config (OSSConfig | None)

  • modelstudio_config (ModelstudioConfig | None)

  • build_root (str | Path | None)

__init__(oss_config=None, modelstudio_config=None, build_root=None, state_manager=None)[source]
Parameters:
  • oss_config (OSSConfig | None)

  • modelstudio_config (ModelstudioConfig | None)

  • build_root (str | Path | None)

Return type:

None

async deploy(app=None, runner=None, endpoint_path='/process', protocol_adapters=None, requirements=None, extra_packages=None, environment=None, project_dir=None, cmd=None, deploy_name=None, skip_upload=False, telemetry_enabled=True, external_whl_path=None, agent_id=None, agent_desc=None, custom_endpoints=None, **kwargs)[source]

Package the project, upload to OSS and trigger ModelStudio deploy.

Returns a dict containing deploy_id, wheel_path, url (if uploaded), resource_name (deploy_name), and workspace_id.

Parameters:
  • endpoint_path (str)

  • protocol_adapters (list[ProtocolAdapter] | None)

  • requirements (str | None | List[str])

  • extra_packages (List[str] | None)

  • environment (Dict[str, str] | None)

  • project_dir (str | Path | None)

  • cmd (str | None)

  • deploy_name (str | None)

  • skip_upload (bool)

  • telemetry_enabled (bool)

  • external_whl_path (str | None)

  • agent_id (str | None)

  • agent_desc (str | None)

  • custom_endpoints (List[Dict] | None)

Return type:

Dict[str, str]

async stop(deploy_id, **kwargs)[source]

Stop ModelStudio deployment.

Note: ModelStudio stop API not yet available.

Parameters:
  • deploy_id (str) – Deployment identifier

  • **kwargs – Additional parameters

Returns:

Dict with success status and message

Return type:

Dict[str, Any]

get_status()[source]
Return type:

str

class agentscope_runtime.engine.deployers.agentrun_deployer.EndpointConfig(agent_runtime_endpoint_name=None, description=None, tags=None, target_version='LATEST')[source]

Bases: object

Configuration for agent runtime endpoint.

Parameters:
  • agent_runtime_endpoint_name (str | None)

  • description (str | None)

  • tags (List[str] | None)

  • target_version (str | None)

agent_runtime_endpoint_name: str | None = None
description: str | None = None
tags: List[str] | None = None
target_version: str | None = 'LATEST'
__init__(agent_runtime_endpoint_name=None, description=None, tags=None, target_version='LATEST')
Parameters:
  • agent_runtime_endpoint_name (str | None)

  • description (str | None)

  • tags (List[str] | None)

  • target_version (str | None)

Return type:

None

class agentscope_runtime.engine.deployers.agentrun_deployer.CodeConfig(command=None, oss_bucket_name=None, oss_object_name=None)[source]

Bases: object

Configuration for code-based runtimes.

Parameters:
  • command (List[str] | None)

  • oss_bucket_name (str | None)

  • oss_object_name (str | None)

command: List[str] | None = None
oss_bucket_name: str | None = None
oss_object_name: str | None = None
__init__(command=None, oss_bucket_name=None, oss_object_name=None)
Parameters:
  • command (List[str] | None)

  • oss_bucket_name (str | None)

  • oss_object_name (str | None)

Return type:

None

class agentscope_runtime.engine.deployers.agentrun_deployer.LogConfig(logstore=None, project=None)[source]

Bases: object

Configuration for logging.

Parameters:
  • logstore (str | None)

  • project (str | None)

logstore: str | None = None
project: str | None = None
__init__(logstore=None, project=None)
Parameters:
  • logstore (str | None)

  • project (str | None)

Return type:

None

class agentscope_runtime.engine.deployers.agentrun_deployer.NetworkConfig(network_mode='PUBLIC', security_group_id=None, vpc_id=None, vswitch_ids=None)[source]

Bases: object

Network configuration for the runtime.

Parameters:
  • network_mode (str)

  • security_group_id (str | None)

  • vpc_id (str | None)

  • vswitch_ids (list[str] | None)

network_mode: str = 'PUBLIC'
security_group_id: str | None = None
vpc_id: str | None = None
vswitch_ids: list[str] | None = None
__init__(network_mode='PUBLIC', security_group_id=None, vpc_id=None, vswitch_ids=None)
Parameters:
  • network_mode (str)

  • security_group_id (str | None)

  • vpc_id (str | None)

  • vswitch_ids (list[str] | None)

Return type:

None

class agentscope_runtime.engine.deployers.agentrun_deployer.AgentRunConfig(*, access_key_id=None, access_key_secret=None, region_id='cn-hangzhou', endpoint=None, log_config=None, network_config=NetworkConfig(network_mode='PUBLIC', security_group_id=None, vpc_id=None, vswitch_ids=None), cpu=2.0, memory=2048, execution_role_arn=None, session_concurrency_limit=200, session_idle_timeout_seconds=3600)[source]

Bases: BaseModel

Parameters:
  • access_key_id (str | None)

  • access_key_secret (str | None)

  • region_id (str)

  • endpoint (str | None)

  • log_config (LogConfig | None)

  • network_config (NetworkConfig | None)

  • cpu (float)

  • memory (int)

  • execution_role_arn (str | None)

  • session_concurrency_limit (int | None)

  • session_idle_timeout_seconds (int | None)

access_key_id: str | None
access_key_secret: str | None
region_id: str
endpoint: str | None
log_config: LogConfig | None
network_config: NetworkConfig | None
cpu: float
memory: int
execution_role_arn: str | None
session_concurrency_limit: int | None
session_idle_timeout_seconds: int | None
classmethod from_env()[source]

Create AgentRunConfig from environment variables.

Returns:

Configuration loaded from environment variables.

Return type:

AgentRunConfig

ensure_valid()[source]

Validate that all required configuration fields are present.

Raises:

ValueError – If required environment variables are missing.

Return type:

None

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.deployers.agentrun_deployer.OSSConfig(*, region='cn-hangzhou', access_key_id=None, access_key_secret=None, bucket_name)[source]

Bases: BaseModel

Parameters:
  • region (str)

  • access_key_id (str | None)

  • access_key_secret (str | None)

  • bucket_name (str)

region: str
access_key_id: str | None
access_key_secret: str | None
bucket_name: str
classmethod from_env()[source]

Create OSSConfig from environment variables.

Returns:

Configuration loaded from environment variables.

Return type:

OSSConfig

ensure_valid()[source]

Validate that all required OSS configuration fields are present.

Raises:

RuntimeError – If required AccessKey credentials are missing.

Return type:

None

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.deployers.agentrun_deployer.AgentRunDeployManager(oss_config=None, agentrun_config=None, build_root=None, state_manager=None)[source]

Bases: DeployManager

Manager for deploying agents to Alibaba Cloud AgentRun service.

This class handles the complete deployment workflow including: - Building and packaging agent projects - Uploading artifacts to OSS - Creating and managing agent runtimes - Creating and managing runtime endpoints

Parameters:
  • oss_config (OSSConfig | None)

  • agentrun_config (AgentRunConfig | None)

  • build_root (str | Path | None)

GET_AGENT_RUNTIME_STATUS_MAX_ATTEMPTS = 60
GET_AGENT_RUNTIME_STATUS_INTERVAL = 1
LATEST_VERSION = 'LATEST'
DEFAULT_ENDPOINT_NAME = 'default-endpoint'
__init__(oss_config=None, agentrun_config=None, build_root=None, state_manager=None)[source]

Initialize AgentRun deployment manager.

Parameters:
  • oss_config (OSSConfig | None) – OSS configuration for artifact storage. If None, loads from environment.

  • agentrun_config (AgentRunConfig | None) – AgentRun service configuration. If None, loads from environment.

  • build_root (str | Path | None) – Root directory for build artifacts. If None, uses parent directory of current working directory.

  • state_manager – Deployment state manager. If None, creates a new instance.

async deploy(runner=None, endpoint_path='/process', protocol_adapters=None, requirements=None, extra_packages=None, environment=None, project_dir=None, cmd=None, deploy_name=None, skip_upload=False, external_whl_path=None, agentrun_id=None, custom_endpoints=None, app=None, **kwargs)[source]

Deploy agent to AgentRun service.

Parameters:
  • app – AgentApp instance to deploy.

  • runner – Runner instance containing the agent to deploy.

  • endpoint_path (str) – HTTP endpoint path for the agent service.

  • protocol_adapters (list[ProtocolAdapter] | None) – List of protocol adapters for the agent.

  • requirements (str | None | List[str]) – Python requirements for the agent (file path or list).

  • extra_packages (List[str] | None) – Additional Python packages to install.

  • environment (Dict[str, str] | None) – Environment variables for the runtime.

  • project_dir (str | Path | None) – Project directory to deploy.

  • cmd (str | None) – Command to start the agent application.

  • deploy_name (str | None) – Name for this deployment.

  • skip_upload (bool) – If True, skip uploading to AgentRun service.

  • external_whl_path (str | None) – Path to pre-built wheel file.

  • agentrun_id (str | None) – ID of existing agent to update.

  • custom_endpoints (List[Dict] | None) – Custom endpoints for the agent.

  • **kwargs – Additional deployment parameters.

Returns:

  • success: Whether deployment succeeded

  • message: Status message

  • agentrun_id: ID of the agent runtime

  • agentrun_endpoint_url: Public endpoint URL

  • build_dir: Build directory path

  • wheel_path: Path to wheel file

  • artifact_url: OSS artifact URL

  • url: Console URL for the deployment

  • deploy_id: Deployment ID

  • resource_name: Resource name

Return type:

Dictionary containing deployment results with keys

Raises:
async deploy_to_agentrun(agent_runtime_name, oss_bucket_name, oss_object_name, agentrun_id=None, environment=None)[source]

Deploy agent runtime and endpoint to AgentRun service.

Parameters:
  • agent_runtime_name (str) – Name for the agent runtime.

  • oss_bucket_name (str) – OSS bucket containing the code artifact.

  • oss_object_name (str) – Object key of the code artifact in OSS.

  • agentrun_id (str | None) – AgentRun ID to update deploy to.

  • environment (Dict[str, str] | None) – Environment variables for the runtime.

Returns:

  • success: Whether deployment succeeded

  • agent_runtime_id: ID of the created runtime

  • agent_runtime_endpoint_id: ID of the created endpoint

  • agent_runtime_endpoint_name: Name of the endpoint

  • agent_runtime_public_endpoint_url: Public URL of the endpoint

  • status: Status of the deployment

  • request_id: Request ID for tracking

Return type:

Dictionary containing deployment results

async delete(agent_runtime_id)[source]

Delete an agent runtime on AgentRun.

Parameters:

agent_runtime_id (str) – The ID of the agent runtime to delete.

Returns:

A dictionary containing the delete result with:
  • success (bool): Whether the operation was successful

  • message (str): Status message

  • agent_runtime_id (str): The ID of the deleted agent runtime

  • status (str): The status of the agent runtime

  • status_reason (str): The reason for the status

  • request_id (str): The request ID for tracking

Return type:

Dict[str, Any]

async get_agent_runtime(agent_runtime_id, agent_runtime_version=None)[source]

Get agent runtime details.

Parameters:
  • agent_runtime_id (str) – The ID of the agent runtime.

  • agent_runtime_version (str, optional) – The version of the agent runtime.

Returns:

A dictionary containing the agent runtime details with:
  • success (bool): Whether the operation was successful

  • data (dict): The agent runtime data

  • request_id (str): The request ID for tracking

Return type:

Dict[str, Any]

async create_agent_runtime(agent_runtime_name, artifact_type, cpu, memory, port, code_configuration=None, description=None, environment_variables=None, execution_role_arn=None, log_configuration=None, network_configuration=None, session_concurrency_limit_per_instance=None, session_idle_timeout_seconds=None)[source]

Create an agent runtime on AgentRun.

Parameters:
  • agent_runtime_name (str) – The name of the agent runtime.

  • artifact_type (str) – The type of the artifact.

  • cpu (float) – The CPU allocated to the runtime.

  • memory (int) – The memory allocated to the runtime.

  • port (int) – The port on which the runtime will listen.

  • code_configuration (Optional[CodeConfig]) – Configuration for code-based runtimes.

  • description (Optional[str]) – Description of the agent runtime.

  • environment_variables (Optional[Dict[str, str]]) – Environment variables for the runtime.

  • execution_role_arn (Optional[str]) – The execution role ARN for accessing cloud services.

  • log_configuration (Optional[LogConfig]) – Configuration for logging.

  • network_configuration (Optional[NetworkConfig]) – Network configuration for the runtime, including: - network_mode: The network mode for the runtime - security_group_id: The security group ID for the runtime - vpc_id: The VPC ID for the runtime - vswitch_ids: List of vswitch IDs for the runtime

  • session_concurrency_limit_per_instance (Optional[int]) – Maximum concurrent sessions per instance.

  • session_idle_timeout_seconds (Optional[int]) – Maximum idle timeout for sessions.

Returns:

A dictionary containing the creation result with:
  • success (bool): Whether the operation was successful

  • agent_runtime_id (str): The ID of the created agent runtime

  • status (str): The status of the agent runtime

  • status_reason (str): The reason for the status

  • request_id (str): The request ID for tracking

Return type:

Dict[str, Any]

async update_agent_runtime(agent_runtime_id, agent_runtime_name=None, artifact_type=None, cpu=None, memory=None, port=None, code_configuration=None, description=None, environment_variables=None, execution_role_arn=None, log_configuration=None, network_configuration=None, session_concurrency_limit_per_instance=None, session_idle_timeout_seconds=None)[source]

Update an agent runtime on AgentRun.

Parameters:
  • agent_runtime_id (str) – The ID of the agent runtime to update.

  • agent_runtime_name (Optional[str]) – The name of the agent runtime.

  • artifact_type (Optional[str]) – The type of the artifact.

  • cpu (Optional[float]) – The CPU allocated to the runtime.

  • memory (Optional[int]) – The memory allocated to the runtime.

  • port (Optional[int]) – The port on which the runtime will listen.

  • code_configuration (Optional[CodeConfig]) – Configuration for code-based runtimes.

  • description (Optional[str]) – Description of the agent runtime.

  • environment_variables (Optional[Dict[str, str]]) – Environment variables for the runtime.

  • execution_role_arn (Optional[str]) – The execution role ARN for accessing cloud services.

  • log_configuration (Optional[LogConfig]) – Configuration for logging.

  • network_configuration (Optional[NetworkConfig]) – Network configuration for the runtime, including: - network_mode: The network mode for the runtime - security_group_id: The security group ID for the runtime - vpc_id: The VPC ID for the runtime - vswitch_ids: List of vswitch IDs for the runtime

  • session_concurrency_limit_per_instance (Optional[int]) – Maximum concurrent sessions per instance.

  • session_idle_timeout_seconds (Optional[int]) – Maximum idle timeout for sessions.

Returns:

A dictionary containing the update result with:
  • success (bool): Whether the operation was successful

  • agent_runtime_id (str): The ID of the updated agent runtime

  • status (str): The status of the agent runtime

  • status_reason (str): The reason for the status

  • request_id (str): The request ID for tracking

Return type:

Dict[str, Any]

async create_agent_runtime_endpoint(agent_runtime_id, endpoint_config=None)[source]

Create an agent runtime endpoint.

Parameters:
  • agent_runtime_id (str) – The ID of the agent runtime.

  • endpoint_config (Optional[EndpointConfig]) – Configuration for the endpoint, including: - agent_runtime_endpoint_name: The name of the endpoint - description: Description of the endpoint - target_version: Target version for the endpoint

Returns:

A dictionary containing the creation result with:
  • success (bool): Whether the operation was successful

  • agent_runtime_endpoint_id (str): The ID of the created endpoint

  • agent_runtime_endpoint_name (str): The name of the created endpoint

  • agent_runtime_public_endpoint_url (str): The public URL of the endpoint

  • status (str): The status of the endpoint

  • status_reason (str): The reason for the status

  • request_id (str): The request ID for tracking

Return type:

Dict[str, Any]

async update_agent_runtime_endpoint(agent_runtime_id, agent_runtime_endpoint_id, endpoint_config=None)[source]

Update an agent runtime endpoint.

Parameters:
  • agent_runtime_id (str) – The ID of the agent runtime.

  • agent_runtime_endpoint_id (str) – The ID of the agent runtime endpoint.

  • endpoint_config (Optional[EndpointConfig]) – Configuration for the endpoint, including: - agent_runtime_endpoint_name: The name of the endpoint - description: Description of the endpoint - target_version: Target version for the endpoint

Returns:

A dictionary containing the update result with:
  • success (bool): Whether the operation was successful

  • agent_runtime_endpoint_id (str): The ID of the updated endpoint

  • status (str): The status of the endpoint

  • status_reason (str): The reason for the status

  • request_id (str): The request ID for tracking

Return type:

Dict[str, Any]

async get_agent_runtime_endpoint(agent_runtime_id, agent_runtime_endpoint_id)[source]

Get an agent runtime endpoint.

Parameters:
  • agent_runtime_id (str) – The ID of the agent runtime.

  • agent_runtime_endpoint_id (str) – The ID of the agent runtime endpoint.

Returns:

A dictionary containing the endpoint details with:
  • success (bool): Whether the operation was successful

  • agent_runtime_endpoint_id (str): The ID of the endpoint

  • agent_runtime_endpoint_name (str): The name of the endpoint

  • agent_runtime_id (str): The ID of the agent runtime

  • agent_runtime_public_endpoint_url (str): The public URL of the endpoint

  • status (str): The status of the endpoint

  • status_reason (str): The reason for the status

  • request_id (str): The request ID for tracking

Return type:

Dict[str, Any]

async delete_agent_runtime_endpoint(agent_runtime_id, agent_runtime_endpoint_id)[source]

Delete an agent runtime endpoint.

Parameters:
  • agent_runtime_id (str) – The ID of the agent runtime.

  • agent_runtime_endpoint_id (str) – The ID of the agent runtime endpoint.

Returns:

A dictionary containing the delete result with:
  • success (bool): Whether the operation was successful

  • message (str): Status message

  • agent_runtime_endpoint_id (str): The ID of the deleted endpoint

  • request_id (str): The request ID for tracking

Return type:

Dict[str, Any]

async publish_agent_runtime_version(agent_runtime_id, description=None)[source]

Publish an agent runtime version.

Parameters:
  • agent_runtime_id (str) – The ID of the agent runtime.

  • description (Optional[str]) – Description of the version.

Returns:

A dictionary containing the publish result with:
  • success (bool): Whether the operation was successful

  • agent_runtime_id (str): The ID of the agent runtime

  • agent_runtime_version (str): The published version

  • description (str): Description of the version

  • request_id (str): The request ID for tracking

Return type:

Dict[str, Any]

async stop(deploy_id, **kwargs)[source]

Stop AgentRun deployment by deleting it.

Parameters:
  • deploy_id (str) – AgentRun runtime ID (agent_runtime_id)

  • **kwargs – Additional parameters

Returns:

Dict with success status, message, and details

Return type:

Dict[str, Any]

agentscope_runtime.engine.deployers.cli_fc_deploy.main()[source]
Return type:

None

Deployers · Adapter

class agentscope_runtime.engine.deployers.adapter.protocol_adapter.ProtocolAdapter(**kwargs)[source]

Bases: ABC

__init__(**kwargs)[source]
abstract add_endpoint(app, func, **kwargs)[source]

Add an endpoint to the protocol adapter.

This method should be implemented by subclasses to provide protocol-specific endpoint addition functionality.

Parameters:
  • *args – Variable length argument list for endpoint configuration

  • **kwargs – Arbitrary keyword arguments for endpoint configuration

  • func (Callable)

Returns:

The result of adding the endpoint, implementation-dependent

Return type:

Any

class agentscope_runtime.engine.deployers.adapter.a2a.a2a_agent_adapter.A2AExecutor(func, **kwargs)[source]

Bases: AgentExecutor

Parameters:

func (Callable)

__init__(func, **kwargs)[source]
Parameters:

func (Callable)

async execute(context, event_queue)[source]

Execute the agent’s logic for a given request context.

The agent should read necessary information from the context and publish Task or Message events, or TaskStatusUpdateEvent / TaskArtifactUpdateEvent to the event_queue. This method should return once the agent’s execution for this request is complete or yields control (e.g., enters an input-required state).

Parameters:
  • context (RequestContext) – The request context containing the message, task ID, etc.

  • event_queue (EventQueue) – The queue to publish events to.

Return type:

None

async cancel(context, event_queue)[source]

Request the agent to cancel an ongoing task.

The agent should attempt to stop the task identified by the task_id in the context and publish a TaskStatusUpdateEvent with state TaskState.canceled to the event_queue.

Parameters:
  • context (RequestContext) – The request context containing the task ID to cancel.

  • event_queue (EventQueue) – The queue to publish the cancellation status update to.

Return type:

None

class agentscope_runtime.engine.deployers.adapter.a2a.a2a_protocol_adapter.A2AFastAPIDefaultAdapter(agent_name, agent_description, **kwargs)[source]

Bases: ProtocolAdapter

__init__(agent_name, agent_description, **kwargs)[source]
add_endpoint(app, func, **kwargs)[source]

Add an endpoint to the protocol adapter.

This method should be implemented by subclasses to provide protocol-specific endpoint addition functionality.

Parameters:
  • *args – Variable length argument list for endpoint configuration

  • **kwargs – Arbitrary keyword arguments for endpoint configuration

  • func (Callable)

Returns:

The result of adding the endpoint, implementation-dependent

Return type:

Any

get_agent_card(agent_name, agent_description)[source]
Parameters:
  • agent_name (str)

  • agent_description (str)

Return type:

AgentCard

agentscope_runtime.engine.deployers.adapter.a2a.a2a_adapter_utils.a2a_message_to_agent_message(msg)[source]

Convert A2A Message object to AgentAPI Message object

Parameters:

msg (A2AMessage) – A2A protocol message object

Returns:

Converted internal Agent API message object

Return type:

AgentMessage

agentscope_runtime.engine.deployers.adapter.a2a.a2a_adapter_utils.a2a_part_to_agent_content(part)[source]

Convert A2A protocol Part object to internal Content object

Parameters:

part (Part) – A2A protocol part object

Returns:

Converted internal content object

Return type:

Content

Raises:

ValueError – If the part type is unknown

agentscope_runtime.engine.deployers.adapter.a2a.a2a_adapter_utils.a2a_sendparams_to_agent_request(params, stream, context_id=None)[source]

Convert a2a MessageSendParams to agent-api AgentRequest

Parameters:
  • params (dict) – MessageSendParams received from a2a protocol

  • stream (bool) – Whether this request is in stream mode (/message/send = False, /message/stream = True)

  • context_id (str, optional) – Context ID if message is appended to existing conversation

Returns:

Converted agent request object

Return type:

AgentRequest

agentscope_runtime.engine.deployers.adapter.a2a.a2a_adapter_utils.a2a_taskqueryparams_to_agent_request(params, session_id=None)[source]

Convert TaskQueryParams to AgentRequest, only set session_id Other fields are controlled by AgentRequest default values

Parameters:
  • params (TaskQueryParams) – Task query parameters from a2a protocol

  • session_id (str, optional) – Session ID for the request

Returns:

Converted agent request object with only session_id set

Return type:

AgentRequest

agentscope_runtime.engine.deployers.adapter.a2a.a2a_adapter_utils.agent_content_to_a2a_part(content)[source]

Convert internal Content object to A2A protocol Part object

Parameters:

content (Content) – Internal content object

Returns:

Converted A2A protocol part object

Return type:

Part

Raises:

ValueError – If the content type is unknown

agentscope_runtime.engine.deployers.adapter.a2a.a2a_adapter_utils.agent_message_to_a2a_artifact(msg)[source]

Convert AgentAPI Message to a2a Artifact

Parameters:

msg (AgentMessage) – Agent API message object

Returns:

Converted A2A protocol artifact object

Return type:

Artifact

agentscope_runtime.engine.deployers.adapter.a2a.a2a_adapter_utils.runstatus_to_a2a_taskstate(status)[source]

Map Internal RunStatus to a2a TaskState

Parameters:

status (str) – Internal run status string

Returns:

Mapped A2A task state

Return type:

TaskState

agentscope_runtime.engine.deployers.adapter.a2a.a2a_adapter_utils.agent_response_to_a2a_task(resp)[source]

Convert AgentResponse object to a2a Task object.

Parameters:

resp (AgentResponse) – Internal agent response object

Returns:

Converted A2A protocol task object

Return type:

Task

agentscope_runtime.engine.deployers.adapter.a2a.a2a_adapter_utils.response_to_task_status_update_event(response)[source]

Convert AgentResponse (internal response) to a2a TaskStatusUpdateEvent.

Parameters:

response (AgentResponse) – Internal agent response object

Returns:

Converted A2A protocol task status update event

Return type:

TaskStatusUpdateEvent

agentscope_runtime.engine.deployers.adapter.a2a.a2a_adapter_utils.content_to_task_artifact_update_event(content, context_id='', task_id=None, append=False, last_chunk=False)[source]

Convert single Content (TextContent/ImageContent/DataContent) to TaskArtifactUpdateEvent. If delta=false, should not return task_artifact_update_event

Parameters:
  • content (Content) – SSE returned content, including delta and non-delta types

  • context_id (str) – Corresponds to agent api sessionId, needs external input

  • task_id (str | None) – Currently equivalent to msg_id, or not passed

  • append (bool) – Used to determine if current artifact is new or first

  • last_chunk (bool) – Used to determine if current content is the last one

Returns:

Converted A2A protocol task artifact update event

Return type:

TaskArtifactUpdateEvent

agentscope_runtime.engine.deployers.adapter.a2a.a2a_adapter_utils.agent_role_to_a2a_role(role)[source]
Parameters:

role (str)

agentscope_runtime.engine.deployers.adapter.a2a.a2a_adapter_utils.agent_message_to_a2a_message(msg)[source]

Convert AgentAPI Message object to a2a protocol Message object

Parameters:

msg (AgentAPIMessage) – Agent API message object

Returns:

Converted A2A protocol message object

Return type:

A2AMessage

class agentscope_runtime.engine.deployers.adapter.responses.response_api_agent_adapter.ResponseAPIExecutor(func, **kwargs)[source]

Bases: object

Parameters:

func (Callable)

__init__(func, **kwargs)[source]
Parameters:

func (Callable)

async execute(request)[source]
Parameters:

request (Dict)

class agentscope_runtime.engine.deployers.adapter.responses.response_api_protocol_adapter.ResponseAPIDefaultAdapter(**kwargs)[source]

Bases: ProtocolAdapter

__init__(**kwargs)[source]
Return type:

None

add_endpoint(app, func, **kwargs)[source]

Add endpoint to FastAPI app.

Parameters:
  • app (FastAPI) – FastAPI application instance

  • func (Callable) – handler function

  • **kwargs – extra options

Return type:

None

Responses Adapter

Bidirectional protocol converter: Responses API ↔ Agent API

Conversion functions: 1. Responses API request → Agent API request 2. Agent API event → Responses API event 3. Support for streaming and non-streaming conversion

class agentscope_runtime.engine.deployers.adapter.responses.response_api_adapter_utils.ResponsesAdapter[source]

Bases: object

Bidirectional protocol converter: Responses API ↔ Agent API

Main functions: 1. Convert Responses API request → Agent API request 2. Convert Agent API event → Responses API event 3. Convert Responses API event stream → Agent API event stream 4. Handle various message types (text, tool calls, reasoning, etc.)

__init__()[source]
Return type:

None

convert_agent_response_to_responses(agent_response)[source]
Parameters:

agent_response (BaseResponse)

convert_status_to_responses(agent_status)[source]
Parameters:

agent_status (str)

Return type:

Literal[‘completed’, ‘failed’, ‘in_progress’, ‘cancelled’, ‘queued’, ‘incomplete’]

convert_responses_request_to_agent_request(responses_request)[source]

Convert Responses API request to Agent API request

Implement automatic assignment of fields with the same name, then explicitly handle different field names

Parameters:

responses_request (Dict[str, Any]) – OpenAI ResponseCreateParams

Returns:

Agent API request format

Return type:

AgentRequest

convert_agent_event_to_responses_event(agent_event)[source]

Convert Agent API event to Responses API stream event

Parameters:

agent_event (Event) – Agent API Event

Returns:

ResponseStreamEvent or None

Return type:

List[Annotated[ResponseAudioDeltaEvent | ResponseAudioDoneEvent | ResponseAudioTranscriptDeltaEvent | ResponseAudioTranscriptDoneEvent | ResponseCodeInterpreterCallCodeDeltaEvent | ResponseCodeInterpreterCallCodeDoneEvent | ResponseCodeInterpreterCallCompletedEvent | ResponseCodeInterpreterCallInProgressEvent | ResponseCodeInterpreterCallInterpretingEvent | ResponseCompletedEvent | ResponseContentPartAddedEvent | ResponseContentPartDoneEvent | ResponseCreatedEvent | ResponseErrorEvent | ResponseFileSearchCallCompletedEvent | ResponseFileSearchCallInProgressEvent | ResponseFileSearchCallSearchingEvent | ResponseFunctionCallArgumentsDeltaEvent | ResponseFunctionCallArgumentsDoneEvent | ResponseInProgressEvent | ResponseFailedEvent | ResponseIncompleteEvent | ResponseOutputItemAddedEvent | ResponseOutputItemDoneEvent | ResponseReasoningSummaryPartAddedEvent | ResponseReasoningSummaryPartDoneEvent | ResponseReasoningSummaryTextDeltaEvent | ResponseReasoningSummaryTextDoneEvent | ResponseReasoningTextDeltaEvent | ResponseReasoningTextDoneEvent | ResponseRefusalDeltaEvent | ResponseRefusalDoneEvent | ResponseTextDeltaEvent | ResponseTextDoneEvent | ResponseWebSearchCallCompletedEvent | ResponseWebSearchCallInProgressEvent | ResponseWebSearchCallSearchingEvent | ResponseImageGenCallCompletedEvent | ResponseImageGenCallGeneratingEvent | ResponseImageGenCallInProgressEvent | ResponseImageGenCallPartialImageEvent | ResponseMcpCallArgumentsDeltaEvent | ResponseMcpCallArgumentsDoneEvent | ResponseMcpCallCompletedEvent | ResponseMcpCallFailedEvent | ResponseMcpCallInProgressEvent | ResponseMcpListToolsCompletedEvent | ResponseMcpListToolsFailedEvent | ResponseMcpListToolsInProgressEvent | ResponseOutputTextAnnotationAddedEvent | ResponseQueuedEvent | ResponseCustomToolCallInputDeltaEvent | ResponseCustomToolCallInputDoneEvent, PropertyInfo(alias=’None’, format=None, format_template=’None’, discriminator=’type’)]] | None

convert_response_to_agent_events(response)[source]

Convert OpenAI Response object to Agent API Event list

Parameters:

response (Response) – OpenAI Response object

Returns:

Agent API Event list

Return type:

List[Event]

Deployers · Utils

Helpers for working with AgentApp objects inside deployers.

agentscope_runtime.engine.deployers.utils.app_runner_utils.ensure_runner_from_app(app)[source]

Return a Runner extracted from an AgentApp instance.

Builds the runner lazily if the app hasn’t initialized it yet.

Parameters:

app (Any)

Return type:

Optional[‘Runner’]

Deployment modes and configuration for unified FastAPI architecture.

class agentscope_runtime.engine.deployers.utils.deployment_modes.DeploymentMode(value)[source]

Bases: str, Enum

FastAPI application deployment modes.

DAEMON_THREAD = 'daemon_thread'
DETACHED_PROCESS = 'detached_process'
STANDALONE = 'standalone'
__format__(format_spec)

Returns format using actual value type unless __str__ has been overridden.

Shared helpers for building detached deployment bundles.

agentscope_runtime.engine.deployers.utils.detached_app.build_detached_app(*, app=None, runner=None, entrypoint=None, requirements=None, extra_packages=None, output_dir=None, dockerfile_path=None, use_local_runtime=None, platform='unknown', **kwargs)[source]

Create a detached bundle directory ready for execution.

All temporary files are created in cwd/.agentscope_runtime/ by default.

Parameters:
  • app – AgentApp instance to deploy

  • runner – Runner instance to deploy

  • entrypoint (str | None) – Entrypoint specification (e.g., “app.py” or “app.py:handler”)

  • requirements (str | None | List[str]) – Additional pip requirements (string or list)

  • extra_packages (List[str] | None) – Additional Python packages to include

  • output_dir (str | None) – Output directory (creates temp dir if None)

  • dockerfile_path (str | None) – Optional custom Dockerfile path to include

  • use_local_runtime (bool | None) – If True, build and include local runtime wheel. If None (default), auto-detect based on version. Useful for development when runtime is not released.

  • platform (str)

Returns:

Tuple of (project_root_path, project_info)

Return type:

Tuple[str, ProjectInfo]

agentscope_runtime.engine.deployers.utils.detached_app.append_project_requirements(extraction_dir, additional_requirements, use_local_runtime=False)[source]

Append requirements to requirements.txt.

For dev versions or when use_local_runtime=True, builds a wheel from local source and places it in wheels/ subdirectory.

Parameters:
  • extraction_dir (Path) – Directory where requirements.txt will be written

  • additional_requirements (str | list | None) – Additional user requirements

  • use_local_runtime (bool | None) – If True, build and use local runtime wheel. Useful for development when runtime is not released.

Return type:

None

agentscope_runtime.engine.deployers.utils.detached_app.get_bundle_entry_script(bundle_dir)[source]
Parameters:

bundle_dir (str | Path | None)

Return type:

str

Project-based packaging utilities for AgentApp and Runner deployment.

This module provides packaging utilities that support: - Function-based AgentApp deployment with decorators - Runner-based deployment with entrypoint files - Entire project directory packaging - Smart dependency caching - CLI-style and object-style deployment patterns

agentscope_runtime.engine.deployers.utils.package.generate_build_directory(platform='unknown', workspace=None)[source]

Generate a platform-aware build directory with timestamp and random suffix.

Parameters:
  • platform (str) – Deployment platform (k8s, modelstudio, agentrun, local, etc.)

  • workspace (Path | None) – Custom workspace directory (defaults to DEFAULT_BUILD_WORKSPACE)

Returns:

Generated build directory path

Return type:

Path

Example

>>> build_dir = generate_build_directory("modelstudio")
>>> # Returns: .agentscope_runtime/builds/modelstudio_20251207_xxx
class agentscope_runtime.engine.deployers.utils.package.RuntimeParameter(*, name, type, default, help=None, cli_name=None)[source]

Bases: BaseModel

Configuration for a runtime parameter.

Parameters:
name: str
type: str
default: str | int | bool | float | None
help: str | None
cli_name: str | None
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.deployers.utils.package.EntrypointInfo(*, module_name, object_name, object_type, host='0.0.0.0', port=8090, extra_parameters=[])[source]

Bases: BaseModel

Information about the generated entrypoint.

Parameters:
  • module_name (str)

  • object_name (str)

  • object_type (str)

  • host (str)

  • port (int)

  • extra_parameters (List[RuntimeParameter])

module_name: str
object_name: str
object_type: str
host: str
port: int
extra_parameters: List[RuntimeParameter]
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.deployers.utils.package.ProjectInfo(*, project_dir, entrypoint_file, entrypoint_handler, handler_type=None, is_directory_entrypoint=False)[source]

Bases: BaseModel

Information about a project to be packaged.

Parameters:
  • project_dir (str)

  • entrypoint_file (str)

  • entrypoint_handler (str)

  • handler_type (str | None)

  • is_directory_entrypoint (bool)

project_dir: str
entrypoint_file: str
entrypoint_handler: str
handler_type: str | None
is_directory_entrypoint: bool
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

agentscope_runtime.engine.deployers.utils.package.project_dir_extractor(app=None, runner=None)[source]

Extract project directory information from app or runner object.

This function inspects the call stack to find where the app or runner was defined and extracts the project root directory and object name.

Parameters:
  • app – AgentApp instance (optional)

  • runner – Runner instance (optional)

Returns:

ProjectInfo with project directory, entrypoint file, and handler name

Raises:
  • ValueError – If neither app nor runner is provided or project dir

  • cannot be determined

Return type:

ProjectInfo

agentscope_runtime.engine.deployers.utils.package.parse_entrypoint(spec)[source]

Parse entrypoint specification into ProjectInfo.

Supported formats: - “app.py” - File with default handler name “app” - “app.py:my_handler” - File with specific handler name - “project_dir/” - Directory (will auto-detect entrypoint)

Parameters:

spec (str) – Entrypoint specification string

Returns:

ProjectInfo with parsed information

Raises:
  • ValueError – If specification format is invalid or file/dir doesn’t

  • exist

Return type:

ProjectInfo

agentscope_runtime.engine.deployers.utils.package.generate_main_template(entrypoint_info)[source]

Generate main.py template based on object type using Jinja2 templates.

Parameters:

entrypoint_info (EntrypointInfo) – Information about the entrypoint

Returns:

String content for main.py

Raises:
Return type:

str

agentscope_runtime.engine.deployers.utils.package.package_code(source_dir, output_zip, ignore_patterns=None)[source]

Package project source code into a zip file.

Parameters:
  • source_dir (Path) – Source directory to package

  • output_zip (Path) – Output zip file path

  • ignore_patterns (List[str] | None) – Optional ignore patterns (uses defaults if None)

Return type:

None

agentscope_runtime.engine.deployers.utils.package.package(app=None, runner=None, entrypoint=None, output_dir=None, host='0.0.0.0', port=8090, extra_parameters=None, requirements=None, platform='unknown', **kwargs)[source]

Package an AgentApp or Runner for deployment.

This function supports two deployment patterns: 1. Object-style: package(app=my_app) or package(runner=my_runner) 2. Entrypoint-style: package(entrypoint=”app.py”) or package( entrypoint=”project_dir/”)

For object-style deployment, this function will: 1. Extract the project directory containing the app/runner 2. Generate a new main.py that imports and runs the app/runner 3. Package the project with the generated main.py as entrypoint

Build directory naming: - When output_dir=None (default), creates workspace directory with

platform-aware naming

  • Directory format: cwd/.agentscope_runtime/builds/<platform>_

    <timestamp>_<code>/

  • Explicit output_dir uses the provided path

Parameters:
  • app – AgentApp instance (for object-style deployment)

  • runner – Runner instance (for object-style deployment)

  • entrypoint (str | None) – Entrypoint specification (for CLI-style deployment)

  • output_dir (str | None) – Output directory (creates temp dir if None)

  • host (str) – Default host for the service (default: “0.0.0.0”)

  • port (int) – Default port for the service (default: 8090)

  • extra_parameters (List[RuntimeParameter] | None) – Additional runtime parameters to expose via CLI

  • requirements (List[str] | None) – Additional pip requirements

  • platform (str) – Deployment platform (k8s, modelstudio, agentrun, local)

  • **kwargs – Additional keyword arguments (ignored)

Returns:

Tuple of (package_path, project_info) - package_path: Path to the deployment package directory - project_info: ProjectInfo with project metadata

Raises:
Return type:

Tuple[str, ProjectInfo]

Example

>>> # Package with extra parameters
>>> extra_params = [
...     RuntimeParameter(
...         name="log_level",
...         type="str",
...         default="info",
...         help="Logging level"
...     ),
...     RuntimeParameter(
...         name="workers",
...         type="int",
...         default=4,
...         help="Number of worker threads"
...     ),
... ]
>>> package(app=my_app, extra_parameters=extra_params, platform="k8s")

Utilities for packaging a local Python project into a distributable wheel that can be uploaded and deployed by various deployers.

This module extracts and generalizes logic from the legacy test script tests/integrated/test_bailian_fc_deploy/deploy_builder.py so that production deployers can reuse the behaviour in a structured way.

agentscope_runtime.engine.deployers.utils.wheel_packager.get_user_bundle_appdir(build_root, user_project_dir)[source]
Parameters:
  • build_root (Path)

  • user_project_dir (Path)

Return type:

Path

agentscope_runtime.engine.deployers.utils.wheel_packager.generate_wrapper_project(build_root, user_project_dir, start_cmd, deploy_name, telemetry_enabled=True, requirements=None)[source]

Create a wrapper project under build_root, embedding user project under user_bundle/<project_basename>. Returns: (wrapper_project_dir, dist_dir)

Parameters:
  • build_root (Path)

  • user_project_dir (Path)

  • start_cmd (str)

  • deploy_name (str)

  • telemetry_enabled (bool)

  • requirements (str | None | List[str])

Return type:

Tuple[Path, Path]

agentscope_runtime.engine.deployers.utils.wheel_packager.build_wheel(project_dir)[source]

Build a wheel inside an isolated virtual environment to avoid PEP 668 issues. Returns the path to the built wheel.

Parameters:

project_dir (Path)

Return type:

Path

agentscope_runtime.engine.deployers.utils.wheel_packager.default_deploy_name()[source]
Return type:

str

class agentscope_runtime.engine.deployers.utils.docker_image_utils.docker_image_builder.RegistryConfig(*, registry_url='', username=None, password=None, namespace='agentscope-runtime', image_pull_secret=None)[source]

Bases: BaseModel

Container registry configuration

Parameters:
  • registry_url (str)

  • username (str)

  • password (str)

  • namespace (str | None)

  • image_pull_secret (str)

registry_url: str
username: str
password: str
namespace: str | None
image_pull_secret: str
get_full_url()[source]
Return type:

str

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.deployers.utils.docker_image_utils.docker_image_builder.BuildConfig(*, no_cache=False, quiet=False, build_args={}, platform=None, target=None, source_updated=False)[source]

Bases: BaseModel

Configuration for Docker image building

Parameters:
no_cache: bool
quiet: bool
build_args: Dict[str, str]
platform: str | None
target: str | None
source_updated: bool
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.deployers.utils.docker_image_utils.docker_image_builder.DockerImageBuilder[source]

Bases: object

Responsible solely for building and managing Docker images. Separated from project packaging for better separation of concerns.

__init__()[source]

Initialize Docker image builder.

static get_full_name(image_name, image_tag='latest')[source]
Parameters:
  • image_name (str)

  • image_tag (str)

build_image(build_context, image_name, image_tag='latest', dockerfile_path=None, config=None, source_updated=False)[source]

Build Docker image from build context.

Parameters:
  • build_context (str) – Path to build context directory

  • image_name (str) – Name for the Docker image

  • image_tag (str) – Tag for the Docker image

  • dockerfile_path (str | None) – Optional path to Dockerfile (defaults to Dockerfile in context)

  • config (BuildConfig | None) – Build configuration

  • source_updated (bool) – Optional flag to determine if source image should be updated.

Returns:

Full image name with tag

Return type:

str

Raises:
tag_image(image_name, registry_config=None)[source]

Tag image with registry info.

Parameters:
  • image_name (str) – Full image name to push

  • registry_config (RegistryConfig | None) – Optional registry config (uses instance config if None)

Returns:

Full image name with url

Return type:

registry_image_name

push_image(image_name, registry_config=None, quiet=False)[source]

Push image to registry.

Parameters:
  • image_name (str) – Full image name to push

  • registry_config (RegistryConfig | None) – Optional registry config (uses instance config if None)

  • quiet (bool) – Whether to suppress output

Returns:

Full image name that was pushed

Return type:

str

Raises:
build_and_push(build_context, image_name, image_tag='latest', dockerfile_path=None, build_config=None, registry_config=None, source_updated=False)[source]

Build and push image in one operation.

Parameters:
  • build_context (str) – Path to build context directory

  • image_name (str) – Name for the Docker image

  • image_tag (str) – Tag for the Docker image

  • dockerfile_path (str | None) – Optional path to Dockerfile

  • build_config (BuildConfig | None) – Build configuration

  • registry_config (RegistryConfig | None) – Registry configuration

  • source_updated (bool) – Whether the source image was updated or not

Returns:

Full registry image name

Return type:

str

remove_image(image_name, force=False, quiet=True)[source]

Remove Docker image.

Parameters:
  • image_name (str) – Name of image to remove

  • force (bool) – Force removal

  • quiet (bool) – Suppress output

Returns:

True if successful

Return type:

bool

get_image_info(image_name)[source]

Get information about a Docker image.

Parameters:

image_name (str) – Name of the Docker image

Returns:

Image information from docker inspect

Return type:

Dict

Raises:

ValueError – If image not found or info invalid

image_exists(image_name)[source]

Check if Docker image exists locally.

Parameters:

image_name (str) – Name of image to check

Returns:

True if image exists

Return type:

bool

class agentscope_runtime.engine.deployers.utils.docker_image_utils.dockerfile_generator.DockerfileConfig(*, base_image='python:3.10-slim-bookworm', port=8000, working_dir='/app', user='appuser', additional_packages=[], env_vars={}, startup_command=None, health_check_endpoint='/health', custom_template=None, platform=None)[source]

Bases: BaseModel

Configuration for Dockerfile generation

Parameters:
  • base_image (str)

  • port (int)

  • working_dir (str)

  • user (str)

  • additional_packages (List[str])

  • env_vars (Dict[str, str])

  • startup_command (str | None)

  • health_check_endpoint (str)

  • custom_template (str | None)

  • platform (str | None)

base_image: str
port: int
working_dir: str
user: str
additional_packages: List[str]
env_vars: Dict[str, str]
startup_command: str | None
health_check_endpoint: str
custom_template: str | None
platform: str | None
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.deployers.utils.docker_image_utils.dockerfile_generator.DockerfileGenerator[source]

Bases: object

Responsible for generating Dockerfiles from templates. Separated from image building for better modularity.

DEFAULT_TEMPLATE = '# Use official Python runtime as base image\nFROM --platform={platform} {base_image}\n\n# Set working directory in container\nWORKDIR /app\n\n# Set environment variables\nENV PYTHONDONTWRITEBYTECODE=1\nENV PYTHONUNBUFFERED=1\n\n# Configure package sources for better performance\nRUN rm -f /etc/apt/sources.list.d/*.list\n\n# add aliyun mirrors\nRUN echo "deb https://mirrors.aliyun.com/debian/ bookworm main contrib "         "non-free non-free-firmware" > /etc/apt/sources.list &&     echo "deb https://mirrors.aliyun.com/debian/ bookworm-updates main "          "contrib non-free non-free-firmware" >> /etc/apt/sources.list &&     echo "deb https://mirrors.aliyun.com/debian-security/ "          "bookworm-security main contrib non-free "          "non-free-firmware" >> /etc/apt/sources.list\n\n# replace debian to aliyun\nRUN mkdir -p /etc/apt/sources.list.d &&     cat > /etc/apt/sources.list.d/debian.sources <<\'EOF\'\nEOF\n\n# Clean up package lists\nRUN rm -rf /var/lib/apt/lists/*\n\n# Install system dependencies\nRUN apt-get update && apt-get install -y \\\n    gcc \\\n    curl \\\n{additional_packages_section}    && rm -rf /var/lib/apt/lists/*\n\n# Copy project files\nCOPY . {working_dir}/\n\n# Install Python dependencies\nRUN pip install --no-cache-dir --upgrade pip\nRUN if [ -f requirements.txt ]; then \\\n        pip install --no-cache-dir -r requirements.txt \\\n        -i https://pypi.tuna.tsinghua.edu.cn/simple; fi\n\n# Create non-root user for security\nRUN adduser --disabled-password --gecos \'\' {user} && \\\n    chown -R {user} {working_dir}\nUSER {user}\n\n{env_vars_section}\n# Expose port\nEXPOSE {port}\n\n# Health check\nHEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \\\n    CMD curl -f http://localhost:{port}{health_check_endpoint} || exit 1\n\n# Command to run the application\n{startup_command_section}'
__init__()[source]
Return type:

None

temp_files: List[str]
generate_dockerfile_content(config)[source]

Generate Dockerfile content from configuration.

Parameters:

config (DockerfileConfig) – Dockerfile configuration

Returns:

Generated Dockerfile content

Return type:

str

create_dockerfile(config, output_dir=None)[source]

Create Dockerfile in specified directory.

Parameters:
  • config (DockerfileConfig) – Dockerfile configuration

  • output_dir (str | None) – Directory to create Dockerfile (temp dir if None)

Returns:

Path to created Dockerfile

Return type:

str

validate_config(config)[source]

Validate Dockerfile configuration.

Parameters:

config (DockerfileConfig) – Configuration to validate

Returns:

True if valid

Return type:

bool

Raises:

ValueError – If configuration is invalid

cleanup()[source]

Clean up temporary files

__enter__()[source]

Context manager entry

__exit__(exc_type, exc_val, exc_tb)[source]

Context manager exit with cleanup

class agentscope_runtime.engine.deployers.utils.docker_image_utils.image_factory.ImageConfig(*, requirements=None, extra_packages=None, build_context_dir=None, endpoint_path='/process', protocol_adapters=None, custom_endpoints=None, base_image='python:3.10-slim-bookworm', port=8000, env_vars=<factory>, startup_command=None, host='0.0.0.0', embed_task_processor=False, extra_startup_args=<factory>, no_cache=False, quiet=False, build_args={}, platform=None, image_name=('agent', ), image_tag=None, registry_config=None, push_to_registry=False)[source]

Bases: BaseModel

Complete configuration for building a Runner image

Parameters:
  • requirements (List[str] | None)

  • extra_packages (List[str] | None)

  • build_context_dir (str | None)

  • endpoint_path (str)

  • protocol_adapters (List | None)

  • custom_endpoints (List[Dict] | None)

  • base_image (str)

  • port (int)

  • env_vars (Dict[str, str])

  • startup_command (str | None)

  • host (str)

  • embed_task_processor (bool)

  • extra_startup_args (Dict[str, str | int | bool])

  • no_cache (bool)

  • quiet (bool)

  • build_args (Dict[str, str])

  • platform (str | None)

  • image_name (str | None)

  • image_tag (str | None)

  • registry_config (RegistryConfig | None)

  • push_to_registry (bool)

requirements: List[str] | None
extra_packages: List[str] | None
build_context_dir: str | None
endpoint_path: str
protocol_adapters: List | None
custom_endpoints: List[Dict] | None
base_image: str
port: int
env_vars: Dict[str, str]
startup_command: str | None
host: str
embed_task_processor: bool
extra_startup_args: Dict[str, str | int | bool]
no_cache: bool
quiet: bool
build_args: Dict[str, str]
platform: str | None
image_name: str | None
image_tag: str | None
registry_config: RegistryConfig | None
push_to_registry: bool
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.deployers.utils.docker_image_utils.image_factory.ImageFactory[source]

Bases: object

Factory class for building Runner Docker images. Coordinates ProjectPackager, DockerfileGenerator, and DockerImageBuilder.

__init__()[source]

Initialize the Runner image factory.

build_image(app=None, runner=None, entrypoint=None, requirements=None, extra_packages=None, base_image='python:3.10-slim-bookworm', image_name='agent', image_tag=None, registry_config=None, push_to_registry=False, protocol_adapters=None, custom_endpoints=None, host='0.0.0.0', embed_task_processor=True, extra_startup_args=None, use_cache=True, **kwargs)[source]

Simplified interface for building Runner images.

All temporary files are created in cwd/.agentscope_runtime/ by default.

Parameters:
  • app – agent app object

  • runner (Runner | None) – Runner object

  • entrypoint (str | None) – Entrypoint specification (e.g., “app.py” or “app.py:handler”)

  • requirements (str | None | List[str]) – Python requirements

  • extra_packages (List[str] | None) – Additional files to include

  • base_image (str) – Docker base image

  • image_name (str) – Docker image name

  • image_tag (str | None) – Optional image tag

  • registry_config (RegistryConfig | None) – Optional registry config

  • push_to_registry (bool) – Whether to push to registry

  • protocol_adapters (List | None) – Protocol adapters

  • custom_endpoints (List[Dict] | None) – Custom endpoints from agent app

  • host (str) – Host to bind to (default: 0.0.0.0 for containers)

  • embed_task_processor (bool) – Whether to embed task processor

  • extra_startup_args (Dict[str, str | int | bool] | None) – Additional startup arguments

  • use_cache (bool) – Enable build cache (default: True)

  • **kwargs – Additional configuration options

Returns:

Built image name

Return type:

str

cleanup()[source]

Clean up all temporary resources

__enter__()[source]

Context manager entry

__exit__(exc_type, exc_val, exc_tb)[source]

Context manager exit with cleanup

async agentscope_runtime.engine.deployers.utils.service_utils.fastapi_factory.error_stream(e)[source]
class agentscope_runtime.engine.deployers.utils.service_utils.fastapi_factory.FastAPIAppFactory[source]

Bases: object

Factory for creating FastAPI applications with unified architecture.

static create_app(func=None, runner=None, endpoint_path='/process', request_model=None, response_type='sse', stream=True, before_start=None, after_finish=None, mode=DeploymentMode.DAEMON_THREAD, protocol_adapters=None, custom_endpoints=None, broker_url=None, backend_url=None, enable_embedded_worker=False, app_kwargs=None, **kwargs)[source]

Create a FastAPI application with unified architecture.

Parameters:
  • func (Callable | None) – Custom processing function

  • runner (Any | None) – Runner instance (for DAEMON_THREAD mode)

  • endpoint_path (str) – API endpoint path for the processing function

  • request_model (Type | None) – Pydantic model for request validation

  • response_type (str) – Response type - “json”, “sse”, or “text”

  • stream (bool) – Enable streaming responses

  • before_start (Callable | None) – Callback function called before server starts

  • after_finish (Callable | None) – Callback function called after server finishes

  • mode (DeploymentMode) – Deployment mode

  • protocol_adapters (list[ProtocolAdapter] | None) – Protocol adapters

  • custom_endpoints (List[Dict] | None) – List of custom endpoint configurations

  • broker_url (str | None) – Celery broker URL

  • backend_url (str | None) – Celery backend URL

  • enable_embedded_worker (bool) – Whether to run embedded Celery worker

  • app_kwargs (Dict | None) – Additional keyword arguments for the FastAPI app

  • **kwargs (Any) – Additional keyword arguments

Returns:

FastAPI application instance

Return type:

FastAPI

FastAPI templates management and rendering.

class agentscope_runtime.engine.deployers.utils.service_utils.fastapi_templates.FastAPITemplateManager[source]

Bases: object

Manager for FastAPI deployment templates.

__init__()[source]

Initialize template manager.

render_standalone_template(agent_name, endpoint_path='/process', deployment_mode=DeploymentMode.STANDALONE, protocol_adapters=None, **kwargs)[source]

Render the standalone deployment template.

Parameters:
  • agent_name (str) – Name of the agent variable

  • endpoint_path (str) – API endpoint path

  • deployment_mode (str) – Deployment mode (standalone or detached_process)

  • protocol_adapters (str | None) – Protocol adapters code string

  • **kwargs – Additional template variables

Returns:

Rendered template content

Return type:

str

render_detached_script_template(endpoint_path='/process', host='127.0.0.1', port=8000, stream_enabled=True, response_type='sse', runner_code='', func_code='', protocol_adapters=None, **kwargs)[source]

Render the detached process script template.

Parameters:
  • endpoint_path (str) – API endpoint path

  • host (str) – Host to bind to

  • port (int) – Port to bind to

  • stream_enabled (bool) – Enable streaming responses

  • response_type (str) – Response type

  • runner_code (str) – Code to setup runner

  • func_code (str) – Code to setup custom function

  • protocol_adapters (str | None) – Protocol adapters code string

  • **kwargs – Additional template variables

Returns:

Rendered template content

Return type:

str

render_template_from_string(template_string, **variables)[source]

Render a template from string.

Parameters:
  • template_string (str) – Template content as string

  • **variables – Template variables

Returns:

Rendered template content

Return type:

str

get_template_list()[source]

Get list of available templates.

Returns:

List of template filenames

Return type:

list

validate_template_variables(template_name, variables)[source]

Validate template variables.

Parameters:
  • template_name (str) – Name of the template

  • variables (Dict[str, Any]) – Variables to validate

Returns:

Dictionary with ‘missing’ and ‘extra’ keys containing lists

Return type:

Dict[str, list]

class agentscope_runtime.engine.deployers.utils.service_utils.process_manager.ProcessManager(shutdown_timeout=30)[source]

Bases: object

Manager for detached process lifecycle.

Parameters:

shutdown_timeout (int)

__init__(shutdown_timeout=30)[source]

Initialize process manager.

Parameters:

shutdown_timeout (int) – Timeout in seconds for graceful shutdown

async start_detached_process(script_path, host='127.0.0.1', port=8000, env=None)[source]

Start a detached process running the given script.

Parameters:
  • script_path (str) – Path to the Python script to run

  • host (str) – Host to bind to

  • port (int) – Port to bind to

  • env (dict | None) – Additional environment variables

Returns:

Process PID

Raises:

RuntimeError – If process creation fails

Return type:

int

async stop_process_gracefully(pid, timeout=None)[source]

Stop a process gracefully.

Parameters:
  • pid (int) – Process ID to stop

  • timeout (int | None) – Timeout for graceful shutdown (uses default if None)

Returns:

True if process was stopped successfully

Raises:

RuntimeError – If process termination fails

Return type:

bool

is_process_running(pid)[source]

Check if a process is running.

Parameters:

pid (int) – Process ID to check

Returns:

True if process is running

Return type:

bool

create_pid_file(pid, file_path)[source]

Create a PID file.

Parameters:
  • pid (int) – Process ID

  • file_path (str) – Path to PID file

Raises:

OSError – If file creation fails

read_pid_file(file_path)[source]

Read PID from file.

Parameters:

file_path (str) – Path to PID file

Returns:

Process ID or None if file doesn’t exist or is invalid

Return type:

int | None

cleanup_pid_file(file_path)[source]

Remove PID file.

Parameters:

file_path (str) – Path to PID file

async find_process_by_port(port)[source]

Find process listening on a specific port.

Parameters:

port (int) – Port number

Returns:

Process ID or None if not found

Return type:

int | None

get_process_info(pid)[source]

Get information about a process.

Parameters:

pid (int) – Process ID

Returns:

Dictionary with process information or None if process not found

Return type:

dict | None

async wait_for_port(host, port, timeout=30)[source]

Wait for a service to become available on a port.

Parameters:
  • host (str) – Host to check

  • port (int) – Port to check

  • timeout (int) – Maximum time to wait

Returns:

True if service becomes available, False if timeout

Return type:

bool

get_process_logs(max_lines=50)[source]

Get the last N lines of process logs.

Parameters:

max_lines (int) – Maximum number of lines to return

Returns:

Log content as string

Return type:

str

cleanup_log_file(keep_file=False)[source]

Clean up log file.

Parameters:

keep_file (bool) – If True, keep the log file on disk but close the handle. If False, delete the log file.

static cleanup_old_logs(max_age_hours=24)[source]

Clean up old log files.

Parameters:

max_age_hours (int) – Remove log files older than this many hours

Services · 核心

class agentscope_runtime.engine.services.base.Service[source]

Bases: ABC

Abstract base class for services.

This class defines the interface that all services must implement.

abstract async start()[source]

Starts the service, initializing any necessary resources or connections.

Return type:

None

abstract async stop()[source]

Stops the service, releasing any acquired resources.

Return type:

None

abstract async health()[source]

Checks the health of the service.

Returns:

True if the service is healthy, False otherwise.

Return type:

bool

class agentscope_runtime.engine.services.base.ServiceLifecycleManagerMixin[source]

Bases: object

Mixin class that provides async lifecycle manager functionality for services.

This mixin can be used with any class that implements the Service interface.

async __aenter__()[source]

Async context manager entry.

async __aexit__(exc_type, exc_val, exc_tb)[source]

Async context manager exit.

class agentscope_runtime.engine.services.base.ServiceWithLifecycleManager[source]

Bases: Service, ServiceLifecycleManagerMixin

Base class for services that want async lifecycle manager functionality.

This class combines the Service interface with the context manager mixin, providing a convenient base class for most service implementations.

Note: This is an abstract base class. Subclasses must implement the abstract methods from the Service class.

abstract async start()[source]

Starts the service, initializing any necessary resources or connections.

Return type:

None

abstract async stop()[source]

Stops the service, releasing any acquired resources.

Return type:

None

abstract async health()[source]

Checks the health of the service.

Returns:

True if the service is healthy, False otherwise.

Return type:

bool

Services · Agent State

class agentscope_runtime.engine.services.agent_state.state_service.StateService[source]

Bases: ServiceWithLifecycleManager

Abstract base class for agent state management services.

Stores and manages agent states organized by user_id, session_id, and round_id. Supports saving, retrieving, listing, and deleting states.

async start()[source]

Starts the service, initializing any necessary resources or connections.

Return type:

None

async stop()[source]

Stops the service, releasing any acquired resources.

Return type:

None

abstract async save_state(user_id, state, session_id=None, round_id=None)[source]

Save serialized state data for a specific user/session.

If round_id is provided, store the state in that round. If round_id is None, append as a new round with automatically assigned round_id.

Parameters:
  • user_id (str) – The unique ID of the user.

  • state (Dict[str, Any]) – A dictionary representing serialized agent state.

  • session_id (str | None) – Optional session/conversation ID. Defaults to “default”.

  • round_id (int | None) – Optional conversation round number.

Returns:

The round_id in which the state was saved.

Return type:

int

abstract async export_state(user_id, session_id=None, round_id=None)[source]

Retrieve serialized state data for a user/session.

If round_id is provided, return that round’s state. If round_id is None, return the latest round’s state.

Parameters:
  • user_id (str) – The unique ID of the user.

  • session_id (str | None) – Optional session/conversation ID.

  • round_id (int | None) – Optional round number.

Returns:

A dictionary representing the agent state, or None if not found.

Return type:

Dict[str, Any] | None

class agentscope_runtime.engine.services.agent_state.state_service.InMemoryStateService[source]

Bases: StateService

In-memory implementation of StateService using dictionaries for sparse round storage.

  • Multiple users, sessions, and non-contiguous round IDs are supported.

  • If round_id is None when saving, a new round is appended automatically.

  • If round_id is None when exporting, the latest round is returned.

__init__()[source]
Return type:

None

async start()[source]

Initialize the in-memory store.

Return type:

None

async stop()[source]

Clear all in-memory state data.

Return type:

None

async health()[source]

Service health check.

Return type:

bool

async save_state(user_id, state, session_id=None, round_id=None)[source]

Save serialized state in sparse dict storage.

If round_id is None, a new round_id will be assigned as (max existing round_id + 1) or 1 if none exist. Otherwise, the given round_id will be overwritten.

Returns:

The round_id where the state was saved.

Parameters:
Return type:

int

async export_state(user_id, session_id=None, round_id=None)[source]

Retrieve state data for given user/session/round.

If round_id is None: return the latest round. If round_id is provided: return that round’s state.

Returns:

Dictionary representing the agent state, or None if not found.

Parameters:
  • user_id (str)

  • session_id (str | None)

  • round_id (int | None)

Return type:

Dict[str, Any] | None

class agentscope_runtime.engine.services.agent_state.redis_state_service.RedisStateService(redis_url='redis://localhost:6379/0', redis_client=None)[source]

Bases: StateService

Redis-based implementation of StateService.

Stores agent states in Redis using a hash per (user_id, session_id), with round_id as the hash field and serialized state as the value.

Parameters:
  • redis_url (str)

  • redis_client (Redis | None)

__init__(redis_url='redis://localhost:6379/0', redis_client=None)[source]
Parameters:
  • redis_url (str)

  • redis_client (Redis | None)

async start()[source]

Initialize the Redis connection.

Return type:

None

async stop()[source]

Close the Redis connection.

Return type:

None

async health()[source]

Service health check.

Return type:

bool

async save_state(user_id, state, session_id=None, round_id=None)[source]

Save serialized state data for a specific user/session.

If round_id is provided, store the state in that round. If round_id is None, append as a new round with automatically assigned round_id.

Parameters:
  • user_id (str) – The unique ID of the user.

  • state (Dict[str, Any]) – A dictionary representing serialized agent state.

  • session_id (str | None) – Optional session/conversation ID. Defaults to “default”.

  • round_id (int | None) – Optional conversation round number.

Returns:

The round_id in which the state was saved.

Return type:

int

async export_state(user_id, session_id=None, round_id=None)[source]

Retrieve serialized state data for a user/session.

If round_id is provided, return that round’s state. If round_id is None, return the latest round’s state.

Parameters:
  • user_id (str) – The unique ID of the user.

  • session_id (str | None) – Optional session/conversation ID.

  • round_id (int | None) – Optional round number.

Returns:

A dictionary representing the agent state, or None if not found.

Return type:

Dict[str, Any] | None

Services · Memory

class agentscope_runtime.engine.services.memory.memory_service.MemoryService[source]

Bases: ServiceWithLifecycleManager

Used to store and retrieve long memory from the database or in-memory. The memory is organized by the user id at top level, under which there are two different memory manage strategies, - one is the message grouped by the session id, the session id is under the user id, - the other is the message grouped by the user id only

abstract async add_memory(user_id, messages, session_id=None)[source]

Adds messages to the memory service.

Parameters:
  • user_id (str) – The user id.

  • messages (list) – The messages to add.

  • session_id (str | None) – The session id, which is optional.

Return type:

None

async stop()[source]

Stops the service, releasing any acquired resources.

async start()[source]

Starts the service, initializing any necessary resources or connections.

abstract async search_memory(user_id, messages, filters=FieldInfo(annotation=NoneType, required=False, default=None, description='Associated filters for the messages, such as top_k, score etc.'))[source]

Searches messages from the memory service.

Parameters:
  • user_id (str) – The user id.

  • messages (list) – The user query or the query with history messages, both in the format of list of messages. If messages is a list, the search will be based on the content of the last message.

  • filters (Dict[str, Any] | None) – The filters used to search memory

Return type:

list

abstract async list_memory(user_id, filters=FieldInfo(annotation=NoneType, required=False, default=None, description='Associated filters for the messages, such as top_k, score etc.'))[source]

Lists the memory items for a given user with filters, such as page_num, page_size, etc.

Parameters:
  • user_id (str) – The user id.

  • filters (Dict[str, Any] | None) – The filters for the memory items.

Return type:

list

abstract async delete_memory(user_id, session_id=None)[source]

Deletes the memory items for a given user with certain session id, or all the memory items for a given user.

Parameters:
  • user_id (str)

  • session_id (str | None)

Return type:

None

class agentscope_runtime.engine.services.memory.memory_service.InMemoryMemoryService[source]

Bases: MemoryService

An in-memory implementation of the memory service.

__init__()[source]

Initializes the InMemorySessionHistoryService.

Return type:

None

async start()[source]

Initialize the in-memory store.

Return type:

None

async stop()[source]

Stops the service.

Return type:

None

async health()[source]

Checks the health of the service.

Return type:

bool

async add_memory(user_id, messages, session_id=None)[source]

Adds messages to the in-memory store.

Parameters:
  • user_id (str) – The user’s unique identifier.

  • messages (list) – A list of messages to be added.

  • session_id (str | None) – An optional session identifier. If not provided,

  • used. (a default session is)

Return type:

None

async search_memory(user_id, messages, filters=None)[source]
Searches messages from the in-memory store for a specific user

based on keywords.

Parameters:
  • user_id (str) – The user’s unique identifier.

  • messages (list) – A list of messages, where the last message’s content is used as the search query.

  • filters (Dict[str, Any] | None) – Optional filters to apply, such as ‘top_k’ to limit the number of returned messages.

Returns:

A list of matching messages from the store.

Return type:

list

async get_query_text(message)[source]

Gets the query text from the messages.

Parameters:

message (Message) – A list of messages.

Returns:

The query text.

Return type:

str

async list_memory(user_id, filters=None)[source]

Lists messages from the in-memory store with pagination support.

Parameters:
  • user_id (str) – The user’s unique identifier.

  • filters (Dict[str, Any] | None) – Optional filters for pagination, including ‘page_num’ and ‘page_size’.

Returns:

A paginated list of messages.

Return type:

list

async delete_memory(user_id, session_id=None)[source]

Deletes messages from the in-memory store.

Parameters:
  • user_id (str) – The user’s unique identifier.

  • session_id (str | None) – If provided, only deletes the messages for that session. Otherwise, deletes all messages for the user.

Return type:

None

class agentscope_runtime.engine.services.memory.mem0_memory_service.Mem0MemoryService(**kwargs)[source]

Bases: MemoryService

Memory service that uses mem0 to store and retrieve memories. To get the api key, please refer to the following link: https://docs.mem0.ai/platform/quickstart

__init__(**kwargs)[source]
async static get_query_text(message)[source]

Get the query text from the message.

Parameters:

message (Message)

Return type:

str

static transform_message(message)[source]
Parameters:

message (Message)

Return type:

dict

async transform_messages(messages)[source]
Parameters:

messages (List[Message])

Return type:

List[dict]

async start()[source]

Starts the service, initializing any necessary resources or connections.

async stop()[source]

Stops the service, releasing any acquired resources.

async health()[source]

Checks the health of the service.

Returns:

True if the service is healthy, False otherwise.

async add_memory(user_id, messages, session_id=None)[source]

Adds messages to the memory service.

Parameters:
  • user_id (str) – The user id.

  • messages (list) – The messages to add.

  • session_id (str | None) – The session id, which is optional.

async search_memory(user_id, messages, filters=None)[source]

Searches messages from the memory service.

Parameters:
  • user_id (str) – The user id.

  • messages (list) – The user query or the query with history messages, both in the format of list of messages. If messages is a list, the search will be based on the content of the last message.

  • filters (Dict[str, Any] | None) – The filters used to search memory

Return type:

list

async list_memory(user_id, filters=None)[source]

Lists the memory items for a given user with filters, such as page_num, page_size, etc.

Parameters:
  • user_id (str) – The user id.

  • filters (Dict[str, Any] | None) – The filters for the memory items.

Return type:

list

async delete_memory(user_id, session_id=None)[source]

Deletes the memory items for a given user with certain session id, or all the memory items for a given user.

Parameters:
  • user_id (str)

  • session_id (str | None)

Return type:

None

class agentscope_runtime.engine.services.memory.redis_memory_service.RedisMemoryService(redis_url='redis://localhost:6379/0', redis_client=None)[source]

Bases: MemoryService

A Redis-based implementation of the memory service.

Parameters:
  • redis_url (str)

  • redis_client (Redis | None)

__init__(redis_url='redis://localhost:6379/0', redis_client=None)[source]
Parameters:
  • redis_url (str)

  • redis_client (Redis | None)

async start()[source]

Starts the Redis connection.

Return type:

None

async stop()[source]

Closes the Redis connection.

Return type:

None

async health()[source]

Checks the health of the service.

Return type:

bool

async add_memory(user_id, messages, session_id=None)[source]

Adds messages to the memory service.

Parameters:
  • user_id (str) – The user id.

  • messages (list) – The messages to add.

  • session_id (str | None) – The session id, which is optional.

Return type:

None

async search_memory(user_id, messages, filters=None)[source]

Searches messages from the memory service.

Parameters:
  • user_id (str) – The user id.

  • messages (list) – The user query or the query with history messages, both in the format of list of messages. If messages is a list, the search will be based on the content of the last message.

  • filters (Dict[str, Any] | None) – The filters used to search memory

Return type:

list

async get_query_text(message)[source]
Parameters:

message (Message)

Return type:

str

async list_memory(user_id, filters=None)[source]

Lists the memory items for a given user with filters, such as page_num, page_size, etc.

Parameters:
  • user_id (str) – The user id.

  • filters (Dict[str, Any] | None) – The filters for the memory items.

Return type:

list

async delete_memory(user_id, session_id=None)[source]

Deletes the memory items for a given user with certain session id, or all the memory items for a given user.

Parameters:
  • user_id (str)

  • session_id (str | None)

Return type:

None

async clear_all_memory()[source]

Clears all memory data from Redis. This method removes all user memory keys from the Redis database.

Return type:

None

async delete_user_memory(user_id)[source]

Deletes all memory data for a specific user.

Parameters:
  • user_id (str) – The ID of the user whose memory data should be

  • deleted

Return type:

None

class agentscope_runtime.engine.services.memory.tablestore_memory_service.SearchStrategy(value)[source]

Bases: Enum

An enumeration.

FULL_TEXT = 'full_text'
VECTOR = 'vector'
class agentscope_runtime.engine.services.memory.tablestore_memory_service.TablestoreMemoryService(tablestore_client, search_strategy=SearchStrategy.FULL_TEXT, embedding_model=None, vector_dimension=1536, table_name='agentscope_runtime_memory', search_index_schema=({'field_name': 'user_id', 'field_type': 4, 'index': null, 'store': null, 'is_array': null, 'enable_sort_and_agg': null, 'analyzer': null, 'analyzer_parameter': null, 'sub_field_schemas': [], 'date_formats': [], 'is_virtual_field': false, 'source_fields': [], 'vector_options': null, 'enable_highlighting': null, 'json_type': null}, {'field_name': 'session_id', 'field_type': 4, 'index': null, 'store': null, 'is_array': null, 'enable_sort_and_agg': null, 'analyzer': null, 'analyzer_parameter': null, 'sub_field_schemas': [], 'date_formats': [], 'is_virtual_field': false, 'source_fields': [], 'vector_options': null, 'enable_highlighting': null, 'json_type': null}), text_field='text', embedding_field='embedding', vector_metric_type=VectorMetricType.VM_COSINE, **kwargs)[source]

Bases: MemoryService

A Tablestore-based implementation of the memory service. based on tablestore_for_agent_memory (https://github.com/aliyun/ alibabacloud-tablestore-for-agent-memory/blob/main/python/docs/knowledge_store_tutorial.ipynb).

Parameters:
  • tablestore_client (AsyncOTSClient)

  • search_strategy (SearchStrategy)

  • embedding_model (Embeddings | None)

  • vector_dimension (int)

  • table_name (str | None)

  • search_index_schema (List[FieldSchema] | None)

  • text_field (str | None)

  • embedding_field (str | None)

  • vector_metric_type (VectorMetricType)

  • kwargs (Any)

__init__(tablestore_client, search_strategy=SearchStrategy.FULL_TEXT, embedding_model=None, vector_dimension=1536, table_name='agentscope_runtime_memory', search_index_schema=({'field_name': 'user_id', 'field_type': 4, 'index': null, 'store': null, 'is_array': null, 'enable_sort_and_agg': null, 'analyzer': null, 'analyzer_parameter': null, 'sub_field_schemas': [], 'date_formats': [], 'is_virtual_field': false, 'source_fields': [], 'vector_options': null, 'enable_highlighting': null, 'json_type': null}, {'field_name': 'session_id', 'field_type': 4, 'index': null, 'store': null, 'is_array': null, 'enable_sort_and_agg': null, 'analyzer': null, 'analyzer_parameter': null, 'sub_field_schemas': [], 'date_formats': [], 'is_virtual_field': false, 'source_fields': [], 'vector_options': null, 'enable_highlighting': null, 'json_type': null}), text_field='text', embedding_field='embedding', vector_metric_type=VectorMetricType.VM_COSINE, **kwargs)[source]
Parameters:
  • tablestore_client (AsyncOTSClient)

  • search_strategy (SearchStrategy)

  • embedding_model (Embeddings | None)

  • vector_dimension (int)

  • table_name (str | None)

  • search_index_schema (List[FieldSchema] | None)

  • text_field (str | None)

  • embedding_field (str | None)

  • vector_metric_type (VectorMetricType)

  • kwargs (Any)

async start()[source]

Start the tablestore service

Return type:

None

async stop()[source]

Close the tablestore service

Return type:

None

async health()[source]

Checks the health of the service.

Return type:

bool

async add_memory(user_id, messages, session_id=None)[source]

Adds messages to the memory service.

Parameters:
  • user_id (str) – The user id.

  • messages (list) – The messages to add.

  • session_id (str | None) – The session id, which is optional.

Return type:

None

async static get_query_text(message)[source]
Parameters:

message (Message)

Return type:

str

async search_memory(user_id, messages, filters=None)[source]

Searches messages from the memory service.

Parameters:
  • user_id (str) – The user id.

  • messages (list) – The user query or the query with history messages, both in the format of list of messages. If messages is a list, the search will be based on the content of the last message.

  • filters (Dict[str, Any] | None) – The filters used to search memory

Return type:

list

async list_memory(user_id, filters=None)[source]

Lists the memory items for a given user with filters, such as page_num, page_size, etc.

Parameters:
  • user_id (str) – The user id.

  • filters (Dict[str, Any] | None) – The filters for the memory items.

Return type:

list

async delete_memory(user_id, session_id=None)[source]

Deletes the memory items for a given user with certain session id, or all the memory items for a given user.

Parameters:
  • user_id (str)

  • session_id (str | None)

Return type:

None

class agentscope_runtime.engine.services.memory.reme_personal_memory_service.ReMePersonalMemoryService(**kwargs)[source]

Bases: MemoryService

ReMe requires the following env variables to be set: FLOW_EMBEDDING_API_KEY=sk-xxxx FLOW_EMBEDDING_BASE_URL=https://xxxx/v1 FLOW_LLM_API_KEY=sk-xxxx FLOW_LLM_BASE_URL=https://xxxx/v1

__init__(**kwargs)[source]
static transform_message(message)[source]
Parameters:

message (Message)

Return type:

dict

transform_messages(messages)[source]
Parameters:

messages (List[Message])

Return type:

List[dict]

async start()[source]

Starts the service, initializing any necessary resources or connections.

Return type:

None

async stop()[source]

Stops the service, releasing any acquired resources.

Return type:

None

async health()[source]

Checks the health of the service.

Returns:

True if the service is healthy, False otherwise.

Return type:

bool

async add_memory(user_id, messages, session_id=None)[source]

Adds messages to the memory service.

Parameters:
  • user_id (str) – The user id.

  • messages (list) – The messages to add.

  • session_id (str | None) – The session id, which is optional.

Return type:

None

async search_memory(user_id, messages, filters=None)[source]

Searches messages from the memory service.

Parameters:
  • user_id (str) – The user id.

  • messages (list) – The user query or the query with history messages, both in the format of list of messages. If messages is a list, the search will be based on the content of the last message.

  • filters (Dict[str, Any] | None) – The filters used to search memory

Return type:

list

async list_memory(user_id, filters=None)[source]

Lists the memory items for a given user with filters, such as page_num, page_size, etc.

Parameters:
  • user_id (str) – The user id.

  • filters (Dict[str, Any] | None) – The filters for the memory items.

Return type:

list

async delete_memory(user_id, session_id=None)[source]

Deletes the memory items for a given user with certain session id, or all the memory items for a given user.

Parameters:
  • user_id (str)

  • session_id (str | None)

Return type:

None

class agentscope_runtime.engine.services.memory.reme_task_memory_service.ReMeTaskMemoryService(**kwargs)[source]

Bases: ReMePersonalMemoryService

__init__(**kwargs)[source]

Services · Session History

class agentscope_runtime.engine.services.session_history.session_history_service.SessionHistoryService[source]

Bases: ServiceWithLifecycleManager

Abstract base class for session history management services.

This class defines the standard interface for creating, retrieving, updating, and deleting conversation sessions. Concrete implementations (like InMemorySessionHistoryService) will handle the actual storage logic.

async start()[source]

Starts the service, initializing any necessary resources or connections.

Return type:

None

async stop()[source]

Stops the service, releasing any acquired resources.

Return type:

None

abstract async create_session(user_id, session_id=None)[source]

Creates a new session for a given user.

Parameters:
  • user_id (str) – The identifier for the user.

  • session_id (str | None) – Could be defined by user

Returns:

The newly created Session object.

Return type:

Session

abstract async get_session(user_id, session_id)[source]

Retrieves a specific session.

Parameters:
  • user_id (str) – The identifier for the user.

  • session_id (str) – The identifier for the session to retrieve.

Returns:

The Session object if found, otherwise should raise an error or return None in concrete implementations.

Return type:

Session | None

abstract async delete_session(user_id, session_id)[source]

Deletes a specific session.

Parameters:
  • user_id (str) – The identifier for the user.

  • session_id (str) – The identifier for the session to delete.

abstract async list_sessions(user_id)[source]

Lists all sessions for a given user.

Parameters:

user_id (str) – The identifier for the user.

Returns:

A list of Session objects.

Return type:

list[Session]

async append_message(session, message)[source]

Appends a message to the history of a specific session.

Parameters:
  • session (Session) – The session to which the message should be appended.

  • message (Message | List[Message] | Dict[str, Any] | List[Dict[str, Any]]) – The message or list of messages to append. Supports both dictionary format and Message objects.

class agentscope_runtime.engine.services.session_history.session_history_service.InMemorySessionHistoryService[source]

Bases: SessionHistoryService

An in-memory implementation of the SessionHistoryService.

This service stores all session data in a dictionary, making it suitable for development, testing, and scenarios where persistence is not required.

_store

A dictionary holding all session objects, keyed by user ID and then by session ID.

__init__()[source]

Initializes the InMemorySessionHistoryService.

Return type:

None

async start()[source]

Initialize the in-memory store.

Return type:

None

async stop()[source]

Clear all in-memory data.

Return type:

None

async health()[source]

Service health check: always True.

Return type:

bool

async create_session(user_id, session_id=None)[source]

Creates a new session for a given user and stores it.

Parameters:
  • user_id (str) – The identifier for the user creating the session.

  • session_id (str | None) – The identifier for the session to delete.

Returns:

A deep copy of the newly created Session object.

Return type:

Session

async get_session(user_id, session_id)[source]

Retrieves a specific session from memory.

Parameters:
  • user_id (str) – The identifier for the user.

  • session_id (str) – The identifier for the session to retrieve.

Returns:

A deep copy of the Session object if found, otherwise None.

Return type:

Session | None

async delete_session(user_id, session_id)[source]

Deletes a specific session from memory.

If the session does not exist, the method does nothing.

Parameters:
  • user_id (str) – The identifier for the user.

  • session_id (str) – The identifier for the session to delete.

Return type:

None

async list_sessions(user_id)[source]

Lists all sessions for a given user.

To improve performance and reduce data transfer, the returned session objects do not contain the detailed response history.

Parameters:

user_id (str) – The identifier of the user whose sessions to list.

Returns:

A list of Session objects belonging to the user, without history.

Return type:

list[Session]

async append_message(session, message)[source]

Appends message to a session’s history in memory.

This method finds the authoritative session object in the in-memory storage and appends the message to its history. It supports both dictionary format messages and Message objects.

Parameters:
  • session (Session) – The session object, typically from the context. The user_id and id from this object are used for lookup.

  • message (Message | List[Message] | Dict[str, Any] | List[Dict[str, Any]]) – The message or list of messages to append to the session’s history.

Return type:

None

class agentscope_runtime.engine.services.session_history.redis_session_history_service.RedisSessionHistoryService(redis_url='redis://localhost:6379/0', redis_client=None)[source]

Bases: SessionHistoryService

Parameters:
  • redis_url (str)

  • redis_client (Redis | None)

__init__(redis_url='redis://localhost:6379/0', redis_client=None)[source]
Parameters:
  • redis_url (str)

  • redis_client (Redis | None)

async start()[source]

Starts the service, initializing any necessary resources or connections.

async stop()[source]

Stops the service, releasing any acquired resources.

async health()[source]

Checks the health of the service.

Returns:

True if the service is healthy, False otherwise.

Return type:

bool

async create_session(user_id, session_id=None)[source]

Creates a new session for a given user.

Parameters:
  • user_id (str) – The identifier for the user.

  • session_id (str | None) – Could be defined by user

Returns:

The newly created Session object.

Return type:

Session

async get_session(user_id, session_id)[source]

Retrieves a specific session.

Parameters:
  • user_id (str) – The identifier for the user.

  • session_id (str) – The identifier for the session to retrieve.

Returns:

The Session object if found, otherwise should raise an error or return None in concrete implementations.

Return type:

Session | None

async delete_session(user_id, session_id)[source]

Deletes a specific session.

Parameters:
  • user_id (str) – The identifier for the user.

  • session_id (str) – The identifier for the session to delete.

async list_sessions(user_id)[source]

Lists all sessions for a given user.

Parameters:

user_id (str) – The identifier for the user.

Returns:

A list of Session objects.

Return type:

list[Session]

async append_message(session, message)[source]

Appends a message to the history of a specific session.

Parameters:
  • session (Session) – The session to which the message should be appended.

  • message (Message | List[Message] | Dict[str, Any] | List[Dict[str, Any]]) – The message or list of messages to append. Supports both dictionary format and Message objects.

async delete_user_sessions(user_id)[source]

Deletes all session history data for a specific user.

Parameters:

user_id (str) – The ID of the user whose session history data should be deleted

Return type:

None

class agentscope_runtime.engine.services.session_history.tablestore_session_history_service.TablestoreSessionHistoryService(tablestore_client, session_table_name='agentscope_runtime_session', message_table_name='agentscope_runtime_message', session_secondary_index_meta=None, session_search_index_schema=None, message_search_index_schema=None, **kwargs)[source]

Bases: SessionHistoryService

An aliyun tablestore implementation of the SessionHistoryService based on tablestore_for_agent_memory (https://github.com/aliyun/ alibabacloud-tablestore-for-agent-memory/blob/main/python/docs/memory_store_tutorial.ipynb).

Parameters:
  • tablestore_client (AsyncOTSClient)

  • session_table_name (str | None)

  • message_table_name (str | None)

  • session_secondary_index_meta (Dict[str, MetaType] | None)

  • session_search_index_schema (List[FieldSchema] | None)

  • message_search_index_schema (List[FieldSchema] | None)

  • kwargs (Any)

__init__(tablestore_client, session_table_name='agentscope_runtime_session', message_table_name='agentscope_runtime_message', session_secondary_index_meta=None, session_search_index_schema=None, message_search_index_schema=None, **kwargs)[source]

Initializes the TablestoreSessionHistoryService.

Parameters:
  • tablestore_client (AsyncOTSClient)

  • session_table_name (str | None)

  • message_table_name (str | None)

  • session_secondary_index_meta (Dict[str, MetaType] | None)

  • session_search_index_schema (List[FieldSchema] | None)

  • message_search_index_schema (List[FieldSchema] | None)

  • kwargs (Any)

Return type:

None

async start()[source]

Start the tablestore service

Return type:

None

async stop()[source]

Close the tablestore service

Return type:

None

async health()[source]

Checks the health of the service.

Return type:

bool

async create_session(user_id, session_id=None)[source]

Creates a new session for a given user and stores it.

Parameters:
  • user_id (str) – The identifier for the user creating the session.

  • session_id (str | None) – The identifier for the session to delete.

Returns:

A newly created Session object.

Return type:

Session

async get_session(user_id, session_id)[source]

Retrieves a specific session from memory.

Parameters:
  • user_id (str) – The identifier for the user.

  • session_id (str) – The identifier for the session to retrieve.

Returns:

A Session object if found, otherwise None.

Return type:

Session | None

async delete_session(user_id, session_id)[source]

Deletes a specific session from memory.

If the session does not exist, the method does nothing.

Parameters:
  • user_id (str) – The identifier for the user.

  • session_id (str) – The identifier for the session to delete.

Return type:

None

async list_sessions(user_id)[source]

Lists all sessions for a given user.

To improve performance and reduce data transfer, the returned session objects do not contain the detailed response history.

Parameters:

user_id (str) – The identifier of the user whose sessions to list.

Returns:

A list of Session objects belonging to the user, without history.

Return type:

list[Session]

async append_message(session, message)[source]

Appends message to a session’s history in memory.

This method finds the authoritative session object in the in-memory storage and appends the message to its history. It supports both dictionary format messages and Message objects.

Parameters:
  • session (Session) – The session object, typically from the context. The user_id and id from this object are used for lookup.

  • message (Message | List[Message] | Dict[str, Any] | List[Dict[str, Any]]) – The message or list of messages to append to the session’s history.

Return type:

None

async delete_user_sessions(user_id)[source]

Deletes all session history data for a specific user.

Parameters:

user_id (str) – The ID of the user whose session history data should be deleted

Return type:

None

Services · Sandbox

class agentscope_runtime.engine.services.sandbox.sandbox_service.SandboxService(base_url=None, bearer_token=None)[source]

Bases: ServiceWithLifecycleManager

__init__(base_url=None, bearer_token=None)[source]
async start()[source]

Starts the service, initializing any necessary resources or connections.

Return type:

None

async stop()[source]

Stops the service, releasing any acquired resources.

Return type:

None

async health()[source]

Checks the health of the service.

Returns:

True if the service is healthy, False otherwise.

Return type:

bool

connect(session_id, user_id=None, sandbox_types=None)[source]
Parameters:
  • session_id (str)

  • user_id (str | None)

Return type:

List

release(session_id, user_id=None)[source]

Services · Utils

agentscope_runtime.engine.services.utils.tablestore_service_utils.create_tablestore_client(end_point, access_key_id, access_key_secret, instance_name, sts_token=None, region=None, credentials_provider=None, retry_policy=<tablestore.retry.WriteRetryPolicy object>, **kwargs)[source]
Parameters:
  • end_point (str)

  • access_key_id (str)

  • access_key_secret (str)

  • instance_name (str)

  • sts_token (str | None)

  • region (str | None)

  • credentials_provider (CredentialsProvider | None)

  • retry_policy (RetryPolicy)

  • kwargs (Any)

Return type:

AsyncOTSClient

agentscope_runtime.engine.services.utils.tablestore_service_utils.exclude_None_fields_in_place(obj)[source]

Remove fields with None values from dictionary in-place

Parameters:

obj (Dict)

agentscope_runtime.engine.services.utils.tablestore_service_utils.stringify_values(d)[source]
Parameters:

d (dict)

Return type:

dict

agentscope_runtime.engine.services.utils.tablestore_service_utils.restore_json_strings(d)[source]
Parameters:

d (dict)

Return type:

dict

agentscope_runtime.engine.services.utils.tablestore_service_utils.tablestore_log(msg)[source]
Parameters:

msg (str)

agentscope_runtime.engine.services.utils.tablestore_service_utils.convert_tablestore_session_to_session(tablestore_session, tablestore_messages=None)[source]

Convert TablestoreSession to Session

Parameters:
  • tablestore_session (Session)

  • tablestore_messages (List[Message] | None)

Return type:

Session

agentscope_runtime.engine.services.utils.tablestore_service_utils.convert_session_to_tablestore_session(session)[source]

Convert Session to TablestoreSession and list of TablestoreMessage

Parameters:

session (Session)

Return type:

Tuple[Session, List[Message]]

agentscope_runtime.engine.services.utils.tablestore_service_utils.convert_tablestore_message_to_message(tablestore_message)[source]

Convert TablestoreMessage to Message

Parameters:

tablestore_message (Message)

Return type:

Message

agentscope_runtime.engine.services.utils.tablestore_service_utils.convert_message_to_tablestore_message(message, session)[source]

Convert Message to TablestoreMessage

Parameters:
  • message (Message)

  • session (Session)

Return type:

Message

agentscope_runtime.engine.services.utils.tablestore_service_utils.convert_messages_to_tablestore_documents(messages, user_id, session_id, embedding_model=None)[source]

Convert list of messages to TablestoreDocuments with optional batch embedding

Parameters:
  • messages (List[Message])

  • user_id (str)

  • session_id (str)

  • embedding_model (Embeddings | None)

Return type:

List[Document]

agentscope_runtime.engine.services.utils.tablestore_service_utils.convert_message_to_tablestore_document(message, user_id, session_id, embedding=None)[source]

Convert Message to TablestoreDocument

Parameters:
  • message (Message)

  • user_id (str)

  • session_id (str)

  • embedding (List[float] | None)

Return type:

Document

agentscope_runtime.engine.services.utils.tablestore_service_utils.convert_tablestore_document_to_message(tablestore_document)[source]

Convert TablestoreDocument to Message

Parameters:

tablestore_document (Document)

Return type:

Message

agentscope_runtime.engine.services.utils.tablestore_service_utils.get_message_metadata_names()[source]

Get list of message metadata field names

Schemas

class agentscope_runtime.engine.schemas.agent_schemas.MessageType[source]

Bases: object

MESSAGE = 'message'
FUNCTION_CALL = 'function_call'
FUNCTION_CALL_OUTPUT = 'function_call_output'
PLUGIN_CALL = 'plugin_call'
PLUGIN_CALL_OUTPUT = 'plugin_call_output'
COMPONENT_CALL = 'component_call'
COMPONENT_CALL_OUTPUT = 'component_call_output'
MCP_LIST_TOOLS = 'mcp_list_tools'
MCP_APPROVAL_REQUEST = 'mcp_approval_request'
MCP_TOOL_CALL = 'mcp_call'
MCP_APPROVAL_RESPONSE = 'mcp_approval_response'
MCP_TOOL_CALL_OUTPUT = 'mcp_call_output'
REASONING = 'reasoning'
HEARTBEAT = 'heartbeat'
ERROR = 'error'
classmethod all_values()[source]

return all constants values in MessageType

class agentscope_runtime.engine.schemas.agent_schemas.ContentType[source]

Bases: object

TEXT = 'text'
DATA = 'data'
IMAGE = 'image'
AUDIO = 'audio'
FILE = 'file'
REFUSAL = 'refusal'
class agentscope_runtime.engine.schemas.agent_schemas.Role[source]

Bases: object

ASSISTANT = 'assistant'
USER = 'user'
SYSTEM = 'system'
TOOL = 'tool'
class agentscope_runtime.engine.schemas.agent_schemas.RunStatus[source]

Bases: object

Enum class for agent event message.

Created = 'created'
InProgress = 'in_progress'
Completed = 'completed'
Canceled = 'canceled'
Failed = 'failed'
Rejected = 'rejected'
Unknown = 'unknown'
Queued = 'queued'
Incomplete = 'incomplete'
class agentscope_runtime.engine.schemas.agent_schemas.FunctionParameters(*, type, properties, required)[source]

Bases: BaseModel

Parameters:
type: str

The type of the parameters object. Must be object.

properties: Dict[str, Any]

The properties of the parameters object.

required: List[str] | None

The names of the required properties.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.agent_schemas.FunctionTool(*, name, description, parameters)[source]

Bases: BaseModel

Model class for message tool.

Parameters:
name: str

The name of the function to be called.

description: str

A description of what the function does, used by the model to choose when and how to call the function.

parameters: FunctionParameters | Dict[str, Any]

The parameters the functions accepts, described as a JSON Schema object.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.agent_schemas.Tool(*, type='function', function=None)[source]

Bases: BaseModel

Model class for assistant message tool call.

Parameters:
  • type (str | None)

  • function (FunctionTool | None)

type: str | None

The type of the tool. Currently, only function is supported.

function: FunctionTool | None

The function that the model called.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.agent_schemas.FunctionCall(*, call_id=None, name=None, arguments=None)[source]

Bases: BaseModel

Model class for assistant prompt message tool call function.

Parameters:
  • call_id (str | None)

  • name (str | None)

  • arguments (str | None)

call_id: str | None

The ID of the tool call.

name: str | None

The name of the function to call.

arguments: str | None

The arguments to call the function with, as generated by the model in JSON format.

Note that the model does not always generate valid JSON, and may hallucinate parameters not defined by your function schema. Validate the arguments in your code before calling your function.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.agent_schemas.FunctionCallOutput(*, call_id, name=None, output)[source]

Bases: BaseModel

Model class for assistant prompt message tool call function.

Parameters:
  • call_id (str)

  • name (str | None)

  • output (str)

call_id: str

The ID of the tool call.

name: str | None

The name of the tool call.

output: str

The result of the function.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.agent_schemas.McpCall(*, call_id=None, arguments=None, name=None, server_label=None)[source]

Bases: BaseModel

MCP TOOL CALL MESSAGE BODY

Parameters:
  • call_id (str | None)

  • arguments (str | None)

  • name (str | None)

  • server_label (str | None)

call_id: str | None

The unique ID of the tool call.

arguments: str | None

A JSON string of the arguments passed to the tool.

name: str | None

The name of the tool that was run.

server_label: str | None

The label of the MCP server running the tool.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.agent_schemas.McpCallOutput(*, call_id, name=None, output)[source]

Bases: BaseModel

MCP TOOL CALL OUTPUT MESSAGE BODY

Parameters:
  • call_id (str)

  • name (str | None)

  • output (str)

call_id: str

The unique ID of the tool call.

name: str | None

The name of the tool call.

output: str

The output from the tool call.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.agent_schemas.McpListToolsTool(*, input_schema, name, annotations=None, description=None)[source]

Bases: BaseModel

Parameters:
input_schema: object

The JSON schema describing the tool’s input.

name: str

The name of the tool.

annotations: object | None

Additional annotations about the tool.

description: str | None

The description of the tool.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.agent_schemas.McpListTools(*, id, server_label, tools, error=None)[source]

Bases: BaseModel

Parameters:
  • id (str)

  • server_label (str)

  • tools (List[McpListToolsTool])

  • error (str | None)

id: str

The unique ID of the list.

server_label: str

The label of the MCP server.

tools: List[McpListToolsTool]

The tools available on the server.

error: str | None

Error message if the server could not list tools.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.agent_schemas.McpApprovalRequest(*, id, arguments, name, server_label)[source]

Bases: BaseModel

mcp approval request

Parameters:
id: str

The unique ID of the approval request.

arguments: str

A json string of arguments for the tool.

name: str

The name of the tool to run.

server_label: str

The label of the mcp server making the request.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.agent_schemas.Error(*, code, message)[source]

Bases: BaseModel

Parameters:
code: str

The error code of the message.

message: str

The error message of the message.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.agent_schemas.Event(*, sequence_number=None, object, status=None, error=None)[source]

Bases: BaseModel

Parameters:
  • sequence_number (int | None)

  • object (str)

  • status (str | None)

  • error (Error | None)

sequence_number: int | None

sequence number of event

object: str

The identity of the content part.

status: str | None

The status of the message. in_progress, completed, or incomplete

error: Error | None

response error for output

created()[source]

Set the message status to ‘created’.

Return type:

Self

in_progress()[source]

Set the message status to ‘in_progress’.

Return type:

Self

completed()[source]

Set the message status to ‘completed’.

Return type:

Self

failed(error)[source]

Set the message status to ‘failed’.

Parameters:

error (Error)

Return type:

Self

rejected()[source]

Set the message status to ‘rejected’.

Return type:

Self

canceled()[source]

Set the message status to ‘canceled’.

Return type:

Self

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.agent_schemas.Content(*, sequence_number=None, object='content', status=None, error=None, type, index=None, delta=False, msg_id=None)[source]

Bases: Event

Parameters:
  • sequence_number (int | None)

  • object (str)

  • status (str | None)

  • error (Error | None)

  • type (str)

  • index (int | None)

  • delta (bool | None)

  • msg_id (str | None)

type: str

The type of the content part.

object: str

The identity of the content part.

index: int | None

the content index in message’s content list

delta: bool | None

Whether this content is a delta.

msg_id: str | None

message unique id

static from_chat_completion_chunk(chunk, index=None)[source]
Parameters:
  • chunk (ChatCompletionChunk)

  • index (int | None)

Return type:

TextContent | DataContent | ImageContent | None

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

sequence_number: int | None

sequence number of event

status: str | None

The status of the message. in_progress, completed, or incomplete

error: Error | None

response error for output

class agentscope_runtime.engine.schemas.agent_schemas.ImageContent(*, sequence_number=None, object='content', status=None, error=None, type='image', index=None, delta=False, msg_id=None, image_url=None)[source]

Bases: Content

Parameters:
  • sequence_number (int | None)

  • object (str)

  • status (str | None)

  • error (Error | None)

  • type (Literal['image'])

  • index (int | None)

  • delta (bool | None)

  • msg_id (str | None)

  • image_url (str | None)

type: Literal['image']

The type of the content part.

image_url: str | None

The image URL details.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

object: str

The identity of the content part.

index: int | None

the content index in message’s content list

delta: bool | None

Whether this content is a delta.

msg_id: str | None

message unique id

sequence_number: int | None

sequence number of event

status: str | None

The status of the message. in_progress, completed, or incomplete

error: Error | None

response error for output

class agentscope_runtime.engine.schemas.agent_schemas.TextContent(*, sequence_number=None, object='content', status=None, error=None, type='text', index=None, delta=False, msg_id=None, text=None)[source]

Bases: Content

Parameters:
  • sequence_number (int | None)

  • object (str)

  • status (str | None)

  • error (Error | None)

  • type (Literal['text'])

  • index (int | None)

  • delta (bool | None)

  • msg_id (str | None)

  • text (str | None)

type: Literal['text']

The type of the content part.

text: str | None

The text content.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

object: str

The identity of the content part.

index: int | None

the content index in message’s content list

delta: bool | None

Whether this content is a delta.

msg_id: str | None

message unique id

sequence_number: int | None

sequence number of event

status: str | None

The status of the message. in_progress, completed, or incomplete

error: Error | None

response error for output

class agentscope_runtime.engine.schemas.agent_schemas.DataContent(*, sequence_number=None, object='content', status=None, error=None, type='data', index=None, delta=False, msg_id=None, data=None)[source]

Bases: Content

Parameters:
  • sequence_number (int | None)

  • object (str)

  • status (str | None)

  • error (Error | None)

  • type (Literal['data'])

  • index (int | None)

  • delta (bool | None)

  • msg_id (str | None)

  • data (Dict | None)

type: Literal['data']

The type of the content part.

data: Dict | None

The data content.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

object: str

The identity of the content part.

index: int | None

the content index in message’s content list

delta: bool | None

Whether this content is a delta.

msg_id: str | None

message unique id

sequence_number: int | None

sequence number of event

status: str | None

The status of the message. in_progress, completed, or incomplete

error: Error | None

response error for output

class agentscope_runtime.engine.schemas.agent_schemas.AudioContent(*, sequence_number=None, object='content', status=None, error=None, type='audio', index=None, delta=False, msg_id=None, data=None, format=None)[source]

Bases: Content

Parameters:
  • sequence_number (int | None)

  • object (str)

  • status (str | None)

  • error (Error | None)

  • type (Literal['audio'])

  • index (int | None)

  • delta (bool | None)

  • msg_id (str | None)

  • data (str | None)

  • format (str | None)

type: Literal['audio']

The type of the content part.

data: str | None

The audio data details.

format: str | None

The format of the audio data.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

object: str

The identity of the content part.

index: int | None

the content index in message’s content list

delta: bool | None

Whether this content is a delta.

msg_id: str | None

message unique id

sequence_number: int | None

sequence number of event

status: str | None

The status of the message. in_progress, completed, or incomplete

error: Error | None

response error for output

class agentscope_runtime.engine.schemas.agent_schemas.FileContent(*, sequence_number=None, object='content', status=None, error=None, type='file', index=None, delta=False, msg_id=None, file_url=None, file_id=None, filename=None, file_data=None)[source]

Bases: Content

Parameters:
  • sequence_number (int | None)

  • object (str)

  • status (str | None)

  • error (Error | None)

  • type (Literal['file'])

  • index (int | None)

  • delta (bool | None)

  • msg_id (str | None)

  • file_url (str | None)

  • file_id (str | None)

  • filename (str | None)

  • file_data (str | None)

type: Literal['file']

The type of the content part.

file_url: str | None

The file URL details.

file_id: str | None

The file ID details.

filename: str | None

The file name details.

file_data: str | None

The file data details.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

object: str

The identity of the content part.

index: int | None

the content index in message’s content list

delta: bool | None

Whether this content is a delta.

msg_id: str | None

message unique id

sequence_number: int | None

sequence number of event

status: str | None

The status of the message. in_progress, completed, or incomplete

error: Error | None

response error for output

class agentscope_runtime.engine.schemas.agent_schemas.RefusalContent(*, sequence_number=None, object='content', status=None, error=None, type='refusal', index=None, delta=False, msg_id=None, refusal=None)[source]

Bases: Content

Parameters:
  • sequence_number (int | None)

  • object (str)

  • status (str | None)

  • error (Error | None)

  • type (Literal['refusal'])

  • index (int | None)

  • delta (bool | None)

  • msg_id (str | None)

  • refusal (str | None)

type: Literal['refusal']

The type of the content part.

refusal: str | None

The refusal content.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

object: str

The identity of the content part.

index: int | None

the content index in message’s content list

delta: bool | None

Whether this content is a delta.

msg_id: str | None

message unique id

sequence_number: int | None

sequence number of event

status: str | None

The status of the message. in_progress, completed, or incomplete

error: Error | None

response error for output

class agentscope_runtime.engine.schemas.agent_schemas.ToolCall(*, arguments, call_id, name)[source]

Bases: BaseModel

Parameters:
arguments: str

A JSON string of the arguments to pass to the function.

call_id: str

The unique ID of the function tool call generated by the model.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

name: str

The name of the function to run.

class agentscope_runtime.engine.schemas.agent_schemas.ToolCallOutput(*, call_id, output)[source]

Bases: BaseModel

Parameters:
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

call_id: str

The unique ID of the function tool call generated by the model.

output: str

A JSON string of the output of the function tool call.

class agentscope_runtime.engine.schemas.agent_schemas.Message(*, sequence_number=None, object='message', status='created', error=None, id=<factory>, type='message', role=None, content=None, code=None, message=None, usage=None, metadata=None)[source]

Bases: Event

Parameters:
  • sequence_number (int | None)

  • object (str)

  • status (str)

  • error (Error | None)

  • id (str)

  • type (str)

  • role (Literal['assistant', 'system', 'user', 'tool'] | None)

  • content (List[Annotated[TextContent | ImageContent | DataContent | AudioContent | FileContent | RefusalContent, FieldInfo(annotation=NoneType, required=True, discriminator='type')]] | None)

  • code (str | None)

  • message (str | None)

  • usage (Dict | None)

  • metadata (Dict | None)

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

sequence_number: int | None

sequence number of event

error: Error | None

response error for output

id: str

message unique id

object: str

message identity

type: str

The type of the message.

status: str

The status of the message. in_progress, completed, or incomplete

role: Literal['assistant', 'system', 'user', 'tool'] | None

The role of the messages author, should be in user,`system`, ‘assistant’.

content: List[Annotated[TextContent | ImageContent | DataContent | AudioContent | FileContent | RefusalContent, FieldInfo(annotation=NoneType, required=True, discriminator='type')]] | None

The contents of the message.

code: str | None

The error code of the message.

message: str | None

The error message of the message.

usage: Dict | None

response usage for output

metadata: Dict | None
static from_openai_message(message)[source]

Create a message object from an openai message.

Parameters:

message (BaseModel | dict)

Return type:

Message

get_text_content()[source]

Extract the first text content from the message.

Returns:

First text string found in the content, or None if no text content

Return type:

str | None

get_image_content()[source]

Extract all image content (URLs or base64 data) from the message.

Returns:

List of image URLs or base64 encoded strings found in the content

Return type:

List[str]

get_audio_content()[source]

Extract all audio content (URLs or base64 data) from the message.

Returns:

List of audio URLs or base64 encoded strings found in the content

Return type:

List[str]

add_delta_content(new_content)[source]
Parameters:

new_content (TextContent | ImageContent | DataContent)

content_completed(content_index)[source]
Parameters:

content_index (int)

add_content(new_content)[source]
Parameters:

new_content (TextContent | ImageContent | DataContent)

class agentscope_runtime.engine.schemas.agent_schemas.BaseRequest(*, input, stream=True, id=None)[source]

Bases: BaseModel

agent request

Parameters:
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

input: List[Message]

input messages

stream: bool

If set, partial message deltas will be sent, like in ChatGPT.

id: str | None

request unique id

class agentscope_runtime.engine.schemas.agent_schemas.AgentRequest(*, input, stream=True, id=None, model=None, top_p=None, temperature=None, frequency_penalty=None, presence_penalty=None, max_tokens=None, stop=None, n=1, seed=None, tools=None, session_id=None, user_id=None, **extra_data)[source]

Bases: BaseRequest

agent request

Parameters:
  • input (List[Message])

  • stream (bool)

  • id (str | None)

  • model (str | None)

  • top_p (float | None)

  • temperature (float | None)

  • frequency_penalty (float | None)

  • presence_penalty (float | None)

  • max_tokens (int | None)

  • stop (str | None | List[str])

  • n (Annotated[int | None, Ge(ge=1), Le(le=5)])

  • seed (int | None)

  • tools (List[Tool | Dict] | None)

  • session_id (str | None)

  • user_id (str | None)

  • extra_data (Any)

input: List[Message]

input messages

stream: bool

If set, partial message deltas will be sent, like in ChatGPT.

id: str | None

request unique id

model_config: ClassVar[ConfigDict] = {'extra': 'allow', 'json_schema_extra': {'example': {'input': [{'content': [{'text': 'hello', 'type': 'text'}], 'role': 'user', 'type': 'message'}, {'content': [{'object': 'content', 'text': 'Hello! How can I assist you today?', 'type': 'text'}], 'role': 'assistant', 'type': 'message'}, {'content': [{'text': 'What is the capital of France?', 'type': 'text'}], 'role': 'user', 'type': 'message'}], 'session_id': '1764056632961'}}}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model: str | None

model id

top_p: float | None

Nucleus sampling, between (0, 1.0], where the model considers the results of the tokens with top_p probability mass.

So 0.1 means only the tokens comprising the top 10% probability mass are considered.

We generally recommend altering this or temperature but not both.

temperature: float | None

What sampling temperature to use, between 0 and 2.

Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.

We generally recommend altering this or top_p but not both.

frequency_penalty: float | None

Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model’s likelihood to repeat the same line verbatim.

presence_penalty: float | None

Number between -2.0 and 2.0.

Positive values penalize new tokens based on whether they appear in the text so far, increasing the model’s likelihood to talk about new topics.

max_tokens: int | None

The maximum number of [tokens](/tokenizer) that can be generated in the chat completion.

The total length of input tokens and generated tokens is limited by the model’s context length.

stop: str | None | List[str]

Up to 4 sequences where the API will stop generating further tokens.

n: int | None

How many chat completion choices to generate for each input message.

Note that you will be charged based on the number of generated tokens across all of the choices. Keep n as 1 to minimize costs.

seed: int | None

If specified, system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result.

tools: List[Tool | Dict] | None

tool call list

session_id: str | None

conversation id for dialog

user_id: str | None

User id for dialog

class agentscope_runtime.engine.schemas.agent_schemas.BaseResponse(*, sequence_number=None, object='response', status='created', error=None, id=<factory>, created_at=1765859462, completed_at=None, output=None, usage=None)[source]

Bases: Event

Parameters:
  • sequence_number (int | None)

  • object (str)

  • status (str)

  • error (Error | None)

  • id (str | None)

  • created_at (int)

  • completed_at (int | None)

  • output (List[Message] | None)

  • usage (Dict | None)

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

sequence_number: int | None

sequence number of event

error: Error | None

response error for output

id: str | None

response unique id

classmethod validate_id(v)[source]
object: str

response identity

status: str

response run status

created_at: int

request start time

completed_at: int | None

request completed time

output: List[Message] | None

response data for output

usage: Dict | None

response usage for output

add_new_message(message)[source]
Parameters:

message (Message)

class agentscope_runtime.engine.schemas.agent_schemas.AgentResponse(*, sequence_number=None, object='response', status='created', error=None, id=<factory>, created_at=1765859462, completed_at=None, output=None, usage=None, session_id=None)[source]

Bases: BaseResponse

agent response

Parameters:
  • sequence_number (int | None)

  • object (str)

  • status (str)

  • error (Error | None)

  • id (str | None)

  • created_at (int)

  • completed_at (int | None)

  • output (List[Message] | None)

  • usage (Dict | None)

  • session_id (str | None)

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

id: str | None

response unique id

object: str

response identity

status: str

response run status

created_at: int

request start time

completed_at: int | None

request completed time

output: List[Message] | None

response data for output

usage: Dict | None

response usage for output

sequence_number: int | None

sequence number of event

error: Error | None

response error for output

session_id: str | None

conversation id for dialog

class agentscope_runtime.engine.schemas.agent_schemas.SequenceNumberGenerator(start=0)[source]

Bases: object

A simple sequence number generator for streaming events.

This class encapsulates the logic for generating sequential numbers, making the code more maintainable and less error-prone.

Parameters:

start (int)

__init__(start=0)[source]

Initialize the generator with a starting number.

Parameters:

start (int) – The starting sequence number (default: 0)

next()[source]

Get the next sequence number and increment the counter.

Returns:

The current sequence number before incrementing

Return type:

int

yield_with_sequence(event)[source]

Set the sequence number on an event and increment the counter.

Parameters:

event (Event) – The event to set the sequence number on

Returns:

The same event with sequence number set

Return type:

Event

agentscope_runtime.engine.schemas.agent_schemas.convert_to_openai_tool_call(function)[source]
Parameters:

function (FunctionCall)

agentscope_runtime.engine.schemas.agent_schemas.convert_to_openai_messages(messages)[source]

Convert a generic message protocol to a model-specific protocol. :param messages: Original list of messages

Returns:

Message format required by the model

Return type:

list

Parameters:

messages (List[Message])

agentscope_runtime.engine.schemas.agent_schemas.convert_to_openai_tools(tools)[source]
Parameters:

tools (List[Tool | Dict])

Return type:

list | None

class agentscope_runtime.engine.schemas.session.Session(*, id, user_id, messages=[])[source]

Bases: BaseModel

Represents a single conversation session.

A session contains the history of a conversation, including all messages, and is uniquely identified by its ID.

Parameters:
id

The unique identifier for the session.

Type:

str

user_id

The identifier of the user who owns the session.

Type:

str

messages

A list of messages formatted for Agent response

Type:

List[agentscope_runtime.engine.schemas.agent_schemas.Message | Dict[str, Any]]

id: str
user_id: str
messages: List[Message | Dict[str, Any]]
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.embedding.Usage(*, prompt_tokens=None, total_tokens=None, input_tokens=None, text_count=None, image_count=None, video_count=None, duration=None)[source]

Bases: BaseModel

Parameters:
  • prompt_tokens (int | None)

  • total_tokens (int | None)

  • input_tokens (int | None)

  • text_count (int | None)

  • image_count (int | None)

  • video_count (int | None)

  • duration (float | None)

prompt_tokens: int | None

The number of tokens used by the prompt.

total_tokens: int | None

The total number of tokens used by the request.

input_tokens: int | None
text_count: int | None
image_count: int | None
video_count: int | None
duration: float | None
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.embedding.EmbeddingResponse(*, data, model, object, usage)[source]

Bases: BaseModel

Parameters:
  • data (List[Embedding])

  • model (str)

  • object (Literal['list'])

  • usage (Usage)

data: List[Embedding]

The list of embeddings generated by the model.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model: str

The name of the model used to generate the embedding.

object: Literal['list']

The object type, which is always “list”.

usage: Usage

The usage information for the request.

class agentscope_runtime.engine.schemas.realtime.AsrVendor(value)[source]

Bases: str, Enum

An enumeration.

MODELSTUDIO = 'modelstudio'
AZURE = 'azure'
__format__(format_spec)

Returns format using actual value type unless __str__ has been overridden.

class agentscope_runtime.engine.schemas.realtime.TtsVendor(value)[source]

Bases: str, Enum

An enumeration.

MODELSTUDIO = 'modelstudio'
AZURE = 'azure'
__format__(format_spec)

Returns format using actual value type unless __str__ has been overridden.

class agentscope_runtime.engine.schemas.realtime.ModelstudioConnection(*, base_url=None, api_key=None, workspace_id=None, user_agent=None, data_inspection=None)[source]

Bases: BaseModel

Parameters:
  • base_url (str | None)

  • api_key (str | None)

  • workspace_id (str | None)

  • user_agent (str | None)

  • data_inspection (str | None)

base_url: str | None
api_key: str | None
workspace_id: str | None
user_agent: str | None
data_inspection: str | None
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.realtime.TtsConfig(*, model=None, voice=None, sample_rate=None, format=None, bits_per_sample=None, nb_channels=None, chat_id=None)[source]

Bases: BaseModel

Parameters:
  • model (str | None)

  • voice (str | None)

  • sample_rate (int | None)

  • format (str | None)

  • bits_per_sample (int | None)

  • nb_channels (int | None)

  • chat_id (str | None)

model: str | None
voice: str | None
sample_rate: int | None
format: str | None
bits_per_sample: int | None
nb_channels: int | None
chat_id: str | None
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.realtime.ModelstudioTtsConfig(*, base_url=None, api_key=None, workspace_id=None, user_agent=None, data_inspection=None, model='cosyvoice-v2', voice='longcheng_v2', sample_rate=16000, format='pcm', bits_per_sample=None, nb_channels=None, chat_id=None)[source]

Bases: TtsConfig, ModelstudioConnection

Parameters:
  • base_url (str | None)

  • api_key (str | None)

  • workspace_id (str | None)

  • user_agent (str | None)

  • data_inspection (str | None)

  • model (str)

  • voice (str)

  • sample_rate (int)

  • format (str | None)

  • bits_per_sample (int | None)

  • nb_channels (int | None)

  • chat_id (str | None)

model: str
voice: str
sample_rate: int
format: str | None
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

bits_per_sample: int | None
nb_channels: int | None
chat_id: str | None
base_url: str | None
api_key: str | None
workspace_id: str | None
user_agent: str | None
data_inspection: str | None
class agentscope_runtime.engine.schemas.realtime.AsrConfig(*, model=None, language=None, sample_rate=None, format=None, bits_per_sample=None, nb_channels=None, initial_silence_timeout=None, max_end_silence=None, fast_vad_min_duration=None, fast_vad_max_duration=None)[source]

Bases: BaseModel

Parameters:
  • model (str | None)

  • language (str | None)

  • sample_rate (int | None)

  • format (str | None)

  • bits_per_sample (int | None)

  • nb_channels (int | None)

  • initial_silence_timeout (int | None)

  • max_end_silence (int | None)

  • fast_vad_min_duration (int | None)

  • fast_vad_max_duration (int | None)

model: str | None
language: str | None
sample_rate: int | None
format: str | None
bits_per_sample: int | None
nb_channels: int | None
initial_silence_timeout: int | None
max_end_silence: int | None
fast_vad_min_duration: int | None
fast_vad_max_duration: int | None
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.realtime.ModelstudioAsrConfig(*, base_url=None, api_key=None, workspace_id=None, user_agent=None, data_inspection=None, model='gummy-realtime-v1', language=None, sample_rate=16000, format='pcm', bits_per_sample=None, nb_channels=None, initial_silence_timeout=None, max_end_silence=700, fast_vad_min_duration=200, fast_vad_max_duration=1100)[source]

Bases: AsrConfig, ModelstudioConnection

Parameters:
  • base_url (str | None)

  • api_key (str | None)

  • workspace_id (str | None)

  • user_agent (str | None)

  • data_inspection (str | None)

  • model (str | None)

  • language (str | None)

  • sample_rate (int | None)

  • format (str | None)

  • bits_per_sample (int | None)

  • nb_channels (int | None)

  • initial_silence_timeout (int | None)

  • max_end_silence (int | None)

  • fast_vad_min_duration (int | None)

  • fast_vad_max_duration (int | None)

model: str | None
sample_rate: int | None
format: str | None
max_end_silence: int | None
fast_vad_min_duration: int | None
fast_vad_max_duration: int | None
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

language: str | None
bits_per_sample: int | None
nb_channels: int | None
initial_silence_timeout: int | None
base_url: str | None
api_key: str | None
workspace_id: str | None
user_agent: str | None
data_inspection: str | None
class agentscope_runtime.engine.schemas.realtime.ModelstudioKnowledgeBaseConfig(*, index_ids, workspace_id, api_key)[source]

Bases: BaseModel

Parameters:
index_ids: List[str]
workspace_id: str
api_key: str
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatUpstream(*, dialog_mode='duplex', enable_server_vad=True, modalities=<factory>, asr_vendor=AsrVendor.MODELSTUDIO, asr_options=AsrConfig(model=None, language=None, sample_rate=None, format=None, bits_per_sample=None, nb_channels=None, initial_silence_timeout=None, max_end_silence=None, fast_vad_min_duration=None, fast_vad_max_duration=None))[source]

Bases: BaseModel

Parameters:
  • dialog_mode (str | None)

  • enable_server_vad (bool | None)

  • modalities (List[str] | None)

  • asr_vendor (AsrVendor | None)

  • asr_options (AsrConfig | None)

dialog_mode: str | None
enable_server_vad: bool | None
modalities: List[str] | None
asr_vendor: AsrVendor | None
asr_options: AsrConfig | None
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatDownstream(*, modalities=<factory>, tts_vendor=TtsVendor.MODELSTUDIO, tts_options=TtsConfig(model=None, voice=None, sample_rate=None, format=None, bits_per_sample=None, nb_channels=None, chat_id=None))[source]

Bases: BaseModel

Parameters:
  • modalities (List[str] | None)

  • tts_vendor (TtsVendor | None)

  • tts_options (TtsConfig | None)

modalities: List[str] | None
tts_vendor: TtsVendor | None
tts_options: TtsConfig | None
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatParameters(*, modelstudio_kb=None, enable_tool_call=False)[source]

Bases: BaseModel

Parameters:
  • modelstudio_kb (ModelstudioKnowledgeBaseConfig | None)

  • enable_tool_call (bool | None)

modelstudio_kb: ModelstudioKnowledgeBaseConfig | None
enable_tool_call: bool | None
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatInput(*, dialog_id=None, app_id=None, text=None)[source]

Bases: BaseModel

Parameters:
  • dialog_id (str | None)

  • app_id (str | None)

  • text (str | None)

dialog_id: str | None
app_id: str | None
text: str | None
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatDirective(value)[source]

Bases: str, Enum

An enumeration.

SESSION_START = 'SessionStart'
SESSION_STOP = 'SessionStop'
__format__(format_spec)

Returns format using actual value type unless __str__ has been overridden.

class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatEvent(value)[source]

Bases: str, Enum

An enumeration.

SESSION_STARTED = 'SessionStarted'
SESSION_STOPPED = 'SessionStopped'
AUDIO_TRANSCRIPT = 'AudioTranscript'
RESPONSE_TEXT = 'ResponseText'
RESPONSE_AUDIO_STARTED = 'ResponseAudioStarted'
RESPONSE_AUDIO_ENDED = 'ResponseAudioEnded'
__format__(format_spec)

Returns format using actual value type unless __str__ has been overridden.

class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatInPayload[source]

Bases: BaseModel

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatSessionStartPayload(*, session_id=<factory>, upstream=ModelstudioVoiceChatUpstream(dialog_mode='duplex', enable_server_vad=True, modalities=['audio'], asr_vendor=<AsrVendor.MODELSTUDIO: 'modelstudio'>, asr_options=AsrConfig(model=None, language=None, sample_rate=None, format=None, bits_per_sample=None, nb_channels=None, initial_silence_timeout=None, max_end_silence=None, fast_vad_min_duration=None, fast_vad_max_duration=None)), downstream=ModelstudioVoiceChatDownstream(modalities=['audio', 'text'], tts_vendor=<TtsVendor.MODELSTUDIO: 'modelstudio'>, tts_options=TtsConfig(model=None, voice=None, sample_rate=None, format=None, bits_per_sample=None, nb_channels=None, chat_id=None)), parameters=ModelstudioVoiceChatParameters(modelstudio_kb=None, enable_tool_call=False))[source]

Bases: ModelstudioVoiceChatInPayload

Parameters:
  • session_id (str | None)

  • upstream (ModelstudioVoiceChatUpstream | None)

  • downstream (ModelstudioVoiceChatDownstream | None)

  • parameters (ModelstudioVoiceChatParameters | None)

session_id: str | None
upstream: ModelstudioVoiceChatUpstream | None
downstream: ModelstudioVoiceChatDownstream | None
parameters: ModelstudioVoiceChatParameters | None
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatSessionStopPayload[source]

Bases: ModelstudioVoiceChatInPayload

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatRequest(*, directive, payload)[source]

Bases: BaseModel

Parameters:
  • directive (ModelstudioVoiceChatDirective)

  • payload (ModelstudioVoiceChatSessionStartPayload | ModelstudioVoiceChatSessionStopPayload)

directive: ModelstudioVoiceChatDirective
payload: ModelstudioVoiceChatSessionStartPayload | ModelstudioVoiceChatSessionStopPayload
parse_payload_based_on_directive(values, handler)[source]
Parameters:
Return type:

None

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatOutPayload(*, session_id=None)[source]

Bases: BaseModel

Parameters:

session_id (str | None)

session_id: str | None
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatSessionStartedPayload(*, session_id=None)[source]

Bases: ModelstudioVoiceChatOutPayload

Parameters:

session_id (str | None)

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

session_id: str | None
class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatSessionStoppedPayload(*, session_id=None)[source]

Bases: ModelstudioVoiceChatOutPayload

Parameters:

session_id (str | None)

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

session_id: str | None
class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatAudioTranscriptPayload(*, session_id=None, text='', finished)[source]

Bases: ModelstudioVoiceChatOutPayload

Parameters:
  • session_id (str | None)

  • text (str | None)

  • finished (bool)

text: str | None
finished: bool
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

session_id: str | None
class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatResponseTextPayload(*, session_id=None, text='', tool_calls=<factory>, finished)[source]

Bases: ModelstudioVoiceChatOutPayload

Parameters:
  • session_id (str | None)

  • text (str | None)

  • tool_calls (List[ChoiceDeltaToolCall] | None)

  • finished (bool)

text: str | None
tool_calls: List[ChoiceDeltaToolCall] | None
finished: bool
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

session_id: str | None
class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatResponseAudioStartedPayload(*, session_id=None)[source]

Bases: ModelstudioVoiceChatOutPayload

Parameters:

session_id (str | None)

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

session_id: str | None
class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatResponseAudioStoppedPayload(*, session_id=None)[source]

Bases: ModelstudioVoiceChatOutPayload

Parameters:

session_id (str | None)

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

session_id: str | None
class agentscope_runtime.engine.schemas.realtime.ModelstudioVoiceChatResponse(*, event, payload)[source]

Bases: BaseModel

Parameters:
  • event (ModelstudioVoiceChatEvent | None)

  • payload (ModelstudioVoiceChatSessionStartedPayload | ModelstudioVoiceChatSessionStoppedPayload | ModelstudioVoiceChatAudioTranscriptPayload | ModelstudioVoiceChatResponseTextPayload | ModelstudioVoiceChatResponseAudioStartedPayload | ModelstudioVoiceChatResponseAudioStoppedPayload)

event: ModelstudioVoiceChatEvent | None
payload: ModelstudioVoiceChatSessionStartedPayload | ModelstudioVoiceChatSessionStoppedPayload | ModelstudioVoiceChatAudioTranscriptPayload | ModelstudioVoiceChatResponseTextPayload | ModelstudioVoiceChatResponseAudioStartedPayload | ModelstudioVoiceChatResponseAudioStoppedPayload
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.realtime.AzureConnection(*, key=None, region=None)[source]

Bases: BaseModel

Parameters:
  • key (str | None)

  • region (str | None)

key: str | None
region: str | None
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.realtime.AzureAsrConfig(*, key=None, region=None, model=None, language='en-US', sample_rate=16000, format='pcm', bits_per_sample=16, nb_channels=1, initial_silence_timeout=5000, max_end_silence=800, fast_vad_min_duration=None, fast_vad_max_duration=None)[source]

Bases: AsrConfig, AzureConnection

Parameters:
  • key (str | None)

  • region (str | None)

  • model (str | None)

  • language (str | None)

  • sample_rate (int | None)

  • format (str | None)

  • bits_per_sample (int | None)

  • nb_channels (int | None)

  • initial_silence_timeout (int | None)

  • max_end_silence (int | None)

  • fast_vad_min_duration (int | None)

  • fast_vad_max_duration (int | None)

sample_rate: int | None
format: str | None
bits_per_sample: int | None
nb_channels: int | None
initial_silence_timeout: int | None
max_end_silence: int | None
language: str | None
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model: str | None
fast_vad_min_duration: int | None
fast_vad_max_duration: int | None
key: str | None
region: str | None
class agentscope_runtime.engine.schemas.realtime.AzureTtsConfig(*, key=None, region=None, model=None, voice='en-US-AvaMultilingualNeural', sample_rate=16000, format='pcm', bits_per_sample=16, nb_channels=1, chat_id=None)[source]

Bases: TtsConfig, AzureConnection

Parameters:
  • key (str | None)

  • region (str | None)

  • model (str | None)

  • voice (str | None)

  • sample_rate (int | None)

  • format (str | None)

  • bits_per_sample (int | None)

  • nb_channels (int | None)

  • chat_id (str | None)

voice: str | None
sample_rate: int | None
format: str | None
bits_per_sample: int | None
nb_channels: int | None
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model: str | None
chat_id: str | None
key: str | None
region: str | None
class agentscope_runtime.engine.schemas.modelstudio_llm.KnowledgeHolder(*, source, content)[source]

Bases: BaseModel

Parameters:
source: str

The source identifier or URL where the knowledge was retrieved from.

content: str

The actual content or knowledge text retrieved from the source.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.modelstudio_llm.IntentionOptions(*, white_list=<factory>, black_list=<factory>, search_model='search_v6', intensity=None, scene_id=None)[source]

Bases: BaseModel

Parameters:
white_list: List[str]

A list of allowed intentions that can be processed.

black_list: List[str]

A list of blocked intentions that should not be processed.

search_model: str

The search model version to use for intentions recognition.

intensity: int | None

The intensity level for intentions matching and processing.

scene_id: str | None

The scene identifier for context-aware intentions processing.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.modelstudio_llm.SearchOptions(*, enable_source=False, enable_citation=False, enable_readpage=False, enable_online_read=False, citation_format='[<number>]', search_strategy='turbo', forced_search=False, prepend_search_result=False, enable_search_extension=False, item_cnt=20000, top_n=0, intention_options=IntentionOptions(white_list=[], black_list=[], search_model='search_v6', intensity=None, scene_id=None))[source]

Bases: BaseModel

Search Options on Modelstudio platform for knowledge retrieval and web search.

Parameters:
  • enable_source (bool)

  • enable_citation (bool)

  • enable_readpage (bool)

  • enable_online_read (bool)

  • citation_format (str)

  • search_strategy (Literal['standard', 'pro_ultra', 'pro', 'lite', 'pro_max', 'image', 'turbo', 'max'])

  • forced_search (bool)

  • prepend_search_result (bool)

  • enable_search_extension (bool)

  • item_cnt (int)

  • top_n (int)

  • intention_options (IntentionOptions | None)

enable_source: bool

Whether to include source information in search results.

enable_citation: bool

Whether to include citation information for retrieved content.

enable_readpage: bool

Whether to enable full page reading for web content.

enable_online_read: bool

Whether to enable online reading capabilities for real-time content.

citation_format: str

The format string for citations in the response.

search_strategy: Literal['standard', 'pro_ultra', 'pro', 'lite', 'pro_max', 'image', 'turbo', 'max']

The search strategy to use (‘standard’, ‘pro_ultra’, ‘pro’, ‘lite’,’pro_max’, ‘image’,’turbo’,’max’).

forced_search: bool

Whether to force search even when cached results are available.

prepend_search_result: bool

Whether to prepend search results to the response.

enable_search_extension: bool

Whether to enable extended search capabilities.

item_cnt: int

The maximum number of items to retrieve in search results.

top_n: int

The number of top results to return (0 means return all).

intention_options: IntentionOptions | None

Options for intentions recognition and processing during search.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.modelstudio_llm.RagOptions(*, workspace_id='', replaced_word='${documents}', index_names=<factory>, pipeline_ids=<factory>, file_id_list=<factory>, prompt_strategy_name='topK', maximum_allowed_chunk_num=5, maximum_allowed_length=2000, prompt_enable_citation=False, fallback_options=None, enable_web_search=False, session_file_ids=<factory>, dense_similarity_top_k=100, sparse_similarity_top_k=100, enable_rewrite=None, rewrite=None, enable_reranking=None, rerank_min_score=None, rerank_top_n=None, rerank=None, enable_reject_filter=None, reject_filter_type=None, reject_filter_model_name=None, reject_filter_prompt=None, enable_agg_search=None, enable_hybrid_gen=None)[source]

Bases: BaseModel

Parameters:
  • workspace_id (str | None)

  • replaced_word (str)

  • index_names (List[str] | None)

  • pipeline_ids (List[str] | None)

  • file_id_list (List[str] | None)

  • prompt_strategy_name (str | None)

  • maximum_allowed_chunk_num (int | None)

  • maximum_allowed_length (int | None)

  • prompt_enable_citation (bool)

  • fallback_options (FallbackOptions | None)

  • enable_web_search (bool)

  • session_file_ids (List[str] | None)

  • dense_similarity_top_k (int | None)

  • sparse_similarity_top_k (int | None)

  • enable_rewrite (bool | None)

  • rewrite (List[RewriteOptions] | None)

  • enable_reranking (bool | None)

  • rerank_min_score (float | None)

  • rerank_top_n (int | None)

  • rerank (List[RerankOptions] | None)

  • enable_reject_filter (bool | None)

  • reject_filter_type (str | None)

  • reject_filter_model_name (str | None)

  • reject_filter_prompt (str | None)

  • enable_agg_search (bool | None)

  • enable_hybrid_gen (bool | None)

model_config: ClassVar[ConfigDict] = {'populate_by_name': True, 'validate_by_alias': True, 'validate_by_name': True}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class FallbackOptions(*, default_response_type='llm', default_response='')[source]

Bases: BaseModel

Parameters:
  • default_response_type (str | None)

  • default_response (str | None)

default_response_type: str | None

The type of default response when RAG fails (‘llm’, ‘template’, ‘none’).

default_response: str | None

The default response text to use when RAG fails.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class RewriteOptions(*, model_name=None, class_name=None)[source]

Bases: BaseModel

Parameters:
  • model_name (str | None)

  • class_name (str | None)

model_name: str | None

The model name to use for rewriting.

class_name: str | None

The class name to use for rewriting.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class RerankOptions(*, model_name=None)[source]

Bases: BaseModel

Parameters:

model_name (str | None)

model_name: str | None

The model name to use for reranking.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

workspace_id: str | None

The modelstudio workspace id

replaced_word: str

The placeholder word in prompts that will be replaced with retrieved documents.

index_names: List[str] | None

List of index names to use for document processing and retrieval.

pipeline_ids: List[str] | None

List of pipeline IDs to use for document processing and retrieval.

file_ids: List[str] | None

List of specific file IDs to search within.

prompt_strategy: str | None

The strategy for selecting and organizing retrieved content in prompts.

maximum_allowed_chunk_num: int | None

The maximum number of document chunks to include in the context.

maximum_allowed_length: int | None

The maximum total length of retrieved content in characters.

enable_citation: bool

Whether to include citation information for retrieved documents.

fallback_options: FallbackOptions | None

Options for handling cases when RAG retrieval fails.

enable_web_search: bool

Whether to enable web search as part of the RAG pipeline.

session_file_ids: List[str] | None

List of file IDs that are specific to the current session.

dense_similarity_top_k: int | None

The number of most similar dense vectors to retrieve.

sparse_similarity_top_k: int | None

The number of most similar sparse vectors to retrieve.

enable_rewrite: bool | None

Whether to enable content rewrite during RAG.

rewrite: List[RewriteOptions] | None

Options for content rewrite.

enable_reranking: bool | None

Whether to enable content reranking.

rerank_min_score: float | None

The minimum score threshold for content reranking.

rerank_top_n: int | None

The number of top results to return for content reranking.

rerank: List[RerankOptions] | None
enable_reject_filter: bool | None

Whether to enable content rejection filtering.

reject_filter_type: str | None

The type of content rejection filter to use.

reject_filter_model_name: str | None

The name of the model to use for content rejection filtering.

reject_filter_prompt: str | None

The prompt to use for content rejection filtering.

enable_agg_search: bool | None

Whether to enable aggregation search.

enable_hybrid_gen: bool | None

Whether to enable hybrid generations.

classmethod prompt_strategy_check(value)[source]
Parameters:

value (str)

Return type:

str

classmethod maximum_allowed_chunk_num_check(value)[source]
Parameters:

value (int)

Return type:

int

class agentscope_runtime.engine.schemas.modelstudio_llm.ModelstudioParameters(*, top_p=None, temperature=None, frequency_penalty=None, presence_penalty=None, max_tokens=None, stop=None, stream=True, stream_options=None, tools=None, tool_choice=None, parallel_tool_calls=False, logit_bias=None, top_logprobs=None, logprobs=None, n=1, seed=None, response_format=ResponseFormat(type='text', json_schema=None), repetition_penalty=None, length_penalty=None, top_k=None, min_tokens=None, result_format='message', incremental_output=False, enable_search=False, search_options=SearchOptions(enable_source=False, enable_citation=False, enable_readpage=False, enable_online_read=False, citation_format='[<number>]', search_strategy='turbo', forced_search=False, prepend_search_result=False, enable_search_extension=False, item_cnt=20000, top_n=0, intention_options=IntentionOptions(white_list=[], black_list=[], search_model='search_v6', intensity=None, scene_id=None)), enable_rag=False, rag_options=None, selected_model='qwen-max', intention_options=None, mcp_config_file=None)[source]

Bases: Parameters

Parameters for Modelstudio platform, extending the base Parameters with Modelstudio-specific options.

Parameters:
  • top_p (float | None)

  • temperature (float | None)

  • frequency_penalty (float | None)

  • presence_penalty (float | None)

  • max_tokens (int | None)

  • stop (str | None | List[str])

  • stream (bool)

  • stream_options (ChatCompletionStreamOptionsParam | None)

  • tools (List[Tool | Dict] | None)

  • tool_choice (str | ToolChoice | None)

  • parallel_tool_calls (bool)

  • logit_bias (Dict[str, int] | None)

  • top_logprobs (int | None)

  • logprobs (bool | None)

  • n (Annotated[int | None, Ge(ge=1), Le(le=5)])

  • seed (int | None)

  • response_format (ResponseFormat | str | None)

  • repetition_penalty (float | None)

  • length_penalty (float | None)

  • top_k (Annotated[int, Strict(strict=True)] | None)

  • min_tokens (int | None)

  • result_format (Literal['text', 'message'])

  • incremental_output (bool)

  • enable_search (bool)

  • search_options (SearchOptions | None)

  • enable_rag (bool)

  • rag_options (RagOptions | None)

  • selected_model (str | None)

  • intention_options (IntentionOptions | None)

  • mcp_config_file (str | None)

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

top_p: float | None

Nucleus sampling, between (0, 1.0], where the model considers the results of the tokens with top_p probability mass.

So 0.1 means only the tokens comprising the top 10% probability mass are considered.

We generally recommend altering this or temperature but not both.

temperature: float | None

What sampling temperature to use, between 0 and 2.

Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.

We generally recommend altering this or top_p but not both.

frequency_penalty: float | None

Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model’s likelihood to repeat the same line verbatim.

presence_penalty: float | None

Number between -2.0 and 2.0.

Positive values penalize new tokens based on whether they appear in the text so far, increasing the model’s likelihood to talk about new topics.

max_tokens: int | None

The maximum number of [tokens](/tokenizer) that can be generated in the chat completion.

The total length of input tokens and generated tokens is limited by the model’s context length.

stop: str | None | List[str] | None

Up to 4 sequences where the API will stop generating further tokens.

stream: bool

If set, partial message deltas will be sent, like in ChatGPT.

stream_options: ChatCompletionStreamOptionsParam | None

Options for streaming response. Only set this when you set stream: true.

tools: List[Tool | Dict] | None

A list of tools the model may call.

Currently, only functions are supported as a tool. Use this to provide a list of functions the model may generate JSON inputs for.

tool_choice: str | ToolChoice | None

Controls which (if any) tool is called by the model.

parallel_tool_calls: bool

Whether to enable parallel function calling during tool use.

logit_bias: Dict[str, int] | None

Modify the likelihood of specified tokens appearing in the completion.

Accepts a JSON object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token.

top_logprobs: int | None

An integer between 0 and 20 specifying the number of most likely tokens to return at each token position, each with an associated log probability.

logprobs must be set to true if this parameter is used.

logprobs: bool | None

Whether to return log probabilities of the output tokens or not.

If true, returns the log probabilities of each output token returned in the content of message.

n: int | None

How many chat completion choices to generate for each input message.

Note that you will be charged based on the number of generated tokens across all of the choices. Keep n as 1 to minimize costs.

seed: int | None

If specified, system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result.

response_format: ResponseFormat | str | None

An object specifying the format that the model must output.

Setting to { “type”: “json_object” } enables JSON mode, which guarantees the message the model generates is valid JSON.

repetition_penalty: float | None

Penalty for repeating tokens. Higher values reduce repetition.

length_penalty: float | None

Penalty applied to longer sequences. Affects the length of generated text.

top_k: Annotated[int, Strict(strict=True)] | None

The number of highest probability vocabulary tokens to keep for top-k filtering.

min_tokens: int | None

The minimum number of tokens to generate before stopping.

result_format: Literal['text', 'message']

The format of the response (‘text’ for plain text, ‘message’ for structured message)

incremental_output: bool

Whether to return incremental output during generations.

enable_search: bool

Whether to enable search capabilities for knowledge retrieval.

search_options: SearchOptions | None

Configuration options for search functionality.

enable_rag: bool

Whether to enable Retrieval-Augmented Generation (RAG) for the Modelstudio assistant service.

rag_options: RagOptions | None

Configuration options for RAG functionality.

selected_model: str | None

The selected model name to use for generations.

intention_options: IntentionOptions | None

Options for intentions recognition and processing.

mcp_config_file: str | None

Path to the MCP (Model Context Protocol) configuration file.

class agentscope_runtime.engine.schemas.modelstudio_llm.ModelstudioChatRequest(*, top_p=None, temperature=None, frequency_penalty=None, presence_penalty=None, max_tokens=None, stop=None, stream=True, stream_options=None, tools=None, tool_choice=None, parallel_tool_calls=False, logit_bias=None, top_logprobs=None, logprobs=None, n=1, seed=None, response_format=ResponseFormat(type='text', json_schema=None), repetition_penalty=None, length_penalty=None, top_k=None, min_tokens=None, result_format='message', incremental_output=False, enable_search=False, search_options=SearchOptions(enable_source=False, enable_citation=False, enable_readpage=False, enable_online_read=False, citation_format='[<number>]', search_strategy='turbo', forced_search=False, prepend_search_result=False, enable_search_extension=False, item_cnt=20000, top_n=0, intention_options=IntentionOptions(white_list=[], black_list=[], search_model='search_v6', intensity=None, scene_id=None)), enable_rag=False, rag_options=None, selected_model='qwen-max', intention_options=None, mcp_config_file=None, messages, model)[source]

Bases: ModelstudioParameters

Parameters:
  • top_p (float | None)

  • temperature (float | None)

  • frequency_penalty (float | None)

  • presence_penalty (float | None)

  • max_tokens (int | None)

  • stop (str | None | List[str])

  • stream (bool)

  • stream_options (ChatCompletionStreamOptionsParam | None)

  • tools (List[Tool | Dict] | None)

  • tool_choice (str | ToolChoice | None)

  • parallel_tool_calls (bool)

  • logit_bias (Dict[str, int] | None)

  • top_logprobs (int | None)

  • logprobs (bool | None)

  • n (Annotated[int | None, Ge(ge=1), Le(le=5)])

  • seed (int | None)

  • response_format (ResponseFormat | str | None)

  • repetition_penalty (float | None)

  • length_penalty (float | None)

  • top_k (Annotated[int, Strict(strict=True)] | None)

  • min_tokens (int | None)

  • result_format (Literal['text', 'message'])

  • incremental_output (bool)

  • enable_search (bool)

  • search_options (SearchOptions | None)

  • enable_rag (bool)

  • rag_options (RagOptions | None)

  • selected_model (str | None)

  • intention_options (IntentionOptions | None)

  • mcp_config_file (str | None)

  • messages (List[OpenAIMessage])

  • model (str)

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

repetition_penalty: float | None

Penalty for repeating tokens. Higher values reduce repetition.

length_penalty: float | None

Penalty applied to longer sequences. Affects the length of generated text.

top_k: StrictInt | None

The number of highest probability vocabulary tokens to keep for top-k filtering.

min_tokens: int | None

The minimum number of tokens to generate before stopping.

result_format: Literal['text', 'message']

The format of the response (‘text’ for plain text, ‘message’ for structured message)

incremental_output: bool

Whether to return incremental output during generations.

enable_search: bool

Whether to enable search capabilities for knowledge retrieval.

search_options: SearchOptions | None

Configuration options for search functionality.

enable_rag: bool

Whether to enable Retrieval-Augmented Generation (RAG) for the Modelstudio assistant service.

rag_options: RagOptions | None

Configuration options for RAG functionality.

selected_model: str | None

The selected model name to use for generations.

intention_options: IntentionOptions | None

Options for intentions recognition and processing.

mcp_config_file: str | None

Path to the MCP (Model Context Protocol) configuration file.

top_p: float | None

Nucleus sampling, between (0, 1.0], where the model considers the results of the tokens with top_p probability mass.

So 0.1 means only the tokens comprising the top 10% probability mass are considered.

We generally recommend altering this or temperature but not both.

temperature: float | None

What sampling temperature to use, between 0 and 2.

Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.

We generally recommend altering this or top_p but not both.

frequency_penalty: float | None

Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model’s likelihood to repeat the same line verbatim.

presence_penalty: float | None

Number between -2.0 and 2.0.

Positive values penalize new tokens based on whether they appear in the text so far, increasing the model’s likelihood to talk about new topics.

max_tokens: int | None

The maximum number of [tokens](/tokenizer) that can be generated in the chat completion.

The total length of input tokens and generated tokens is limited by the model’s context length.

stop: str | None | List[str] | None

Up to 4 sequences where the API will stop generating further tokens.

stream: bool

If set, partial message deltas will be sent, like in ChatGPT.

stream_options: ChatCompletionStreamOptionsParam | None

Options for streaming response. Only set this when you set stream: true.

tools: List[Tool | Dict] | None

A list of tools the model may call.

Currently, only functions are supported as a tool. Use this to provide a list of functions the model may generate JSON inputs for.

tool_choice: str | ToolChoice | None

Controls which (if any) tool is called by the model.

parallel_tool_calls: bool

Whether to enable parallel function calling during tool use.

logit_bias: Dict[str, int] | None

Modify the likelihood of specified tokens appearing in the completion.

Accepts a JSON object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token.

top_logprobs: int | None

An integer between 0 and 20 specifying the number of most likely tokens to return at each token position, each with an associated log probability.

logprobs must be set to true if this parameter is used.

logprobs: bool | None

Whether to return log probabilities of the output tokens or not.

If true, returns the log probabilities of each output token returned in the content of message.

n: int | None

How many chat completion choices to generate for each input message.

Note that you will be charged based on the number of generated tokens across all of the choices. Keep n as 1 to minimize costs.

seed: int | None

If specified, system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result.

response_format: ResponseFormat | str | None

An object specifying the format that the model must output.

Setting to { “type”: “json_object” } enables JSON mode, which guarantees the message the model generates is valid JSON.

messages: List[OpenAIMessage]

A list of messages comprising the conversation so far.

model: str

ID of the model to use for the chat completion.

class agentscope_runtime.engine.schemas.modelstudio_llm.ModelstudioChatResponse(*, id, choices, created, model, object, service_tier=None, system_fingerprint=None, usage=None, **extra_data)[source]

Bases: ChatCompletion

Parameters:
  • id (str)

  • choices (List[Choice])

  • created (int)

  • model (str)

  • object (Literal['chat.completion'])

  • service_tier (Literal['auto', 'default', 'flex', 'scale', 'priority'] | None)

  • system_fingerprint (str | None)

  • usage (CompletionUsage | None)

  • extra_data (Any)

model_config: ClassVar[ConfigDict] = {'defer_build': True, 'extra': 'allow'}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

id: str

A unique identifier for the chat completion.

choices: List[Choice]

A list of chat completion choices.

Can be more than one if n is greater than 1.

created: int

The Unix timestamp (in seconds) of when the chat completion was created.

model: str

The model used for the chat completion.

object: Literal['chat.completion']

The object type, which is always chat.completion.

service_tier: Literal['auto', 'default', 'flex', 'scale', 'priority'] | None

Specifies the processing type used for serving the request.

  • If set to ‘auto’, then the request will be processed with the service tier configured in the Project settings. Unless otherwise configured, the Project will use ‘default’.

  • If set to ‘default’, then the request will be processed with the standard pricing and performance for the selected model.

  • If set to ‘[flex](https://platform.openai.com/docs/guides/flex-processing)’ or ‘[priority](https://openai.com/api-priority-processing/)’, then the request will be processed with the corresponding service tier.

  • When not set, the default behavior is ‘auto’.

When the service_tier parameter is set, the response body will include the service_tier value based on the processing mode actually used to serve the request. This response value may be different from the value set in the parameter.

system_fingerprint: str | None

This fingerprint represents the backend configuration that the model runs with.

Can be used in conjunction with the seed request parameter to understand when backend changes have been made that might impact determinism.

usage: CompletionUsage | None

Usage statistics for the completion request.

class agentscope_runtime.engine.schemas.modelstudio_llm.ModelstudioChatCompletionChunk(*, id, choices, created, model, object, service_tier=None, system_fingerprint=None, usage=None, **extra_data)[source]

Bases: ChatCompletionChunk

Parameters:
  • id (str)

  • choices (List[Choice])

  • created (int)

  • model (str)

  • object (Literal['chat.completion.chunk'])

  • service_tier (Literal['auto', 'default', 'flex', 'scale', 'priority'] | None)

  • system_fingerprint (str | None)

  • usage (CompletionUsage | None)

  • extra_data (Any)

model_config: ClassVar[ConfigDict] = {'defer_build': True, 'extra': 'allow'}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

id: str

A unique identifier for the chat completion. Each chunk has the same ID.

choices: List[Choice]

A list of chat completion choices.

Can contain more than one elements if n is greater than 1. Can also be empty for the last chunk if you set stream_options: {“include_usage”: true}.

created: int

The Unix timestamp (in seconds) of when the chat completion was created.

Each chunk has the same timestamp.

model: str

The model to generate the completion.

object: Literal['chat.completion.chunk']

The object type, which is always chat.completion.chunk.

service_tier: Literal['auto', 'default', 'flex', 'scale', 'priority'] | None

Specifies the processing type used for serving the request.

  • If set to ‘auto’, then the request will be processed with the service tier configured in the Project settings. Unless otherwise configured, the Project will use ‘default’.

  • If set to ‘default’, then the request will be processed with the standard pricing and performance for the selected model.

  • If set to ‘[flex](https://platform.openai.com/docs/guides/flex-processing)’ or ‘[priority](https://openai.com/api-priority-processing/)’, then the request will be processed with the corresponding service tier.

  • When not set, the default behavior is ‘auto’.

When the service_tier parameter is set, the response body will include the service_tier value based on the processing mode actually used to serve the request. This response value may be different from the value set in the parameter.

system_fingerprint: str | None

This fingerprint represents the backend configuration that the model runs with. Can be used in conjunction with the seed request parameter to understand when backend changes have been made that might impact determinism.

usage: CompletionUsage | None

An optional field that will only be present when you set stream_options: {“include_usage”: true} in your request. When present, it contains a null value except for the last chunk which contains the token usage statistics for the entire request.

NOTE: If the stream is interrupted or cancelled, you may not receive the final usage chunk which contains the total token usage for the request.

agentscope_runtime.engine.schemas.oai_llm.generate_tool_call_id(prefix='call_')[source]
Parameters:

prefix (str)

Return type:

str

class agentscope_runtime.engine.schemas.oai_llm.ImageMessageContent(*, type='image_url', image_url)[source]

Bases: BaseModel

Parameters:
  • type (Literal['image_url'])

  • image_url (ImageUrl)

class ImageUrl(*, url, detail='low')[source]

Bases: BaseModel

Model class for image prompt message content.

Parameters:
  • url (str)

  • detail (Literal['auto', 'low', 'high'])

url: str

Either a URL of the image or the base64 encoded image data.

detail: Literal['auto', 'low', 'high']

Specifies the detail level of the image.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

type: Literal['image_url']

The type of the content part.

image_url: ImageUrl

The image URL details.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.oai_llm.TextMessageContent(*, type='text', text)[source]

Bases: BaseModel

Parameters:
type: Literal['text']

The type of the content part.

text: str

The text content.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.oai_llm.AudioMessageContent(*, type='input_audio', input_audio)[source]

Bases: BaseModel

Parameters:
  • type (Literal['input_audio'])

  • input_audio (InputAudioDetail)

class InputAudioDetail(*, base64_data='', format='mp3')[source]

Bases: BaseModel

Model class for audio prompt message content.

Parameters:
  • base64_data (str)

  • format (str)

base64_data: str

The base64 encoded audio data.

format: str

The format of the encoded audio data. Supports ‘wav’ and ‘mp3’.

property data: str
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

type: Literal['input_audio']

The type of the content part.

input_audio: InputAudioDetail

The input audio details.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.oai_llm.ToolCall(*, index=0, id, type=None, function)[source]

Bases: BaseModel

Model class for assistant prompt message tool call.

Parameters:
  • index (int)

  • id (str)

  • type (str | None)

  • function (FunctionCall)

index: int

The index of the tool call in the tool calls array.

id: str

The ID of the tool call.

type: str | None

The type of the tool. Currently, only function is supported.

function: FunctionCall

The function that the model called.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.oai_llm.OpenAIMessage(*, role, content=None, name=None, tool_calls=None)[source]

Bases: BaseModel

Model class for prompt message.

Parameters:
  • role (str)

  • content (List[Annotated[TextMessageContent | ImageMessageContent | AudioMessageContent, FieldInfo(annotation=NoneType, required=True, discriminator='type')]] | str | None)

  • name (str | None)

  • tool_calls (List[ToolCall] | None)

role: str

The role of the messages author, should be in user,`system`, ‘assistant’, ‘tool’.

content: List[Annotated[TextMessageContent | ImageMessageContent | AudioMessageContent, FieldInfo(annotation=NoneType, required=True, discriminator='type')]] | str | None

The contents of the message.

Can be a string, a list of content parts for multimodal messages.

name: str | None

An optional name for the participant.

Provides the model information to differentiate between participants of the same role.

tool_calls: List[ToolCall] | None

The tool calls generated by the model, such as function calls.

get_text_content()[source]

Extract the first text content from the message.

Returns:

First text string found in the content, or None if no text content.

Return type:

str | None

get_image_content()[source]

Extract all image content (URLs or base64 data) from the message.

Returns:

List of image URLs or base64 encoded strings found in the content.

Return type:

List[str]

get_audio_content()[source]

Extract all audio content (URLs or base64 data) from the message.

Returns:

List of audio URLs or base64 encoded strings found in the content.

Return type:

List[str]

has_multimodal_content()[source]

Check if the message contains multimodal content (images, audio, or video).

Returns:

True if the message contains non-text content, False otherwise

Return type:

bool

get_content_summary()[source]

Get a summary of different content types in the message.

Returns:

Dictionary with counts of different content types

Return type:

Dict[str, int]

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.schemas.oai_llm.UserMessage(*, role='user', content=None, name=None, tool_calls=None)[source]

Bases: OpenAIMessage

Model class for user prompt message.

Parameters:
  • role (str)

  • content (List[Annotated[TextMessageContent | ImageMessageContent | AudioMessageContent, FieldInfo(annotation=NoneType, required=True, discriminator='type')]] | str | None)

  • name (str | None)

  • tool_calls (List[ToolCall] | None)

role: str

The role of the messages author, in this case user.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

content: List[ChatCompletionMessage] | str | None

The contents of the message.

Can be a string, a list of content parts for multimodal messages.

name: str | None

An optional name for the participant.

Provides the model information to differentiate between participants of the same role.

tool_calls: List[ToolCall] | None

The tool calls generated by the model, such as function calls.

class agentscope_runtime.engine.schemas.oai_llm.AssistantMessage(*, role='assistant', content=None, name=None, tool_calls=None)[source]

Bases: OpenAIMessage

Model class for assistant prompt message.

Parameters:
  • role (str)

  • content (List[Annotated[TextMessageContent | ImageMessageContent | AudioMessageContent, FieldInfo(annotation=NoneType, required=True, discriminator='type')]] | str | None)

  • name (str | None)

  • tool_calls (List[ToolCall] | None)

role: str

The role of the messages author, in this case assistant.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

content: List[ChatCompletionMessage] | str | None

The contents of the message.

Can be a string, a list of content parts for multimodal messages.

name: str | None

An optional name for the participant.

Provides the model information to differentiate between participants of the same role.

tool_calls: List[ToolCall] | None

The tool calls generated by the model, such as function calls.

class agentscope_runtime.engine.schemas.oai_llm.SystemMessage(*, role='system', content=None, name=None, tool_calls=None)[source]

Bases: OpenAIMessage

Model class for system prompt message.

Parameters:
  • role (str)

  • content (List[Annotated[TextMessageContent | ImageMessageContent | AudioMessageContent, FieldInfo(annotation=NoneType, required=True, discriminator='type')]] | str | None)

  • name (str | None)

  • tool_calls (List[ToolCall] | None)

role: str

The role of the messages author, in this case system.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

content: List[ChatCompletionMessage] | str | None

The contents of the message.

Can be a string, a list of content parts for multimodal messages.

name: str | None

An optional name for the participant.

Provides the model information to differentiate between participants of the same role.

tool_calls: List[ToolCall] | None

The tool calls generated by the model, such as function calls.

class agentscope_runtime.engine.schemas.oai_llm.ToolMessage(*, role='tool', content=None, name=None, tool_calls=None, tool_call_id)[source]

Bases: OpenAIMessage

Model class for tool prompt message.

Parameters:
  • role (str)

  • content (List[Annotated[TextMessageContent | ImageMessageContent | AudioMessageContent, FieldInfo(annotation=NoneType, required=True, discriminator='type')]] | str | None)

  • name (str | None)

  • tool_calls (List[ToolCall] | None)

  • tool_call_id (str)

role: str

The role of the messages author, in this case tool.

tool_call_id: str

Tool call that this message is responding to.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

content: List[ChatCompletionMessage] | str | None

The contents of the message.

Can be a string, a list of content parts for multimodal messages.

name: str | None

An optional name for the participant.

Provides the model information to differentiate between participants of the same role.

tool_calls: List[ToolCall] | None

The tool calls generated by the model, such as function calls.

class agentscope_runtime.engine.schemas.oai_llm.ResponseFormat(*, type='text', json_schema=None)[source]

Bases: BaseModel

Parameters:
  • type (Literal['text', 'json_object', 'json_schema'])

  • json_schema (JsonSchema | None)

class JsonSchema(*, name, description=None, schema=None, strict=False)[source]

Bases: BaseModel

Parameters:
  • name (str)

  • description (str | None)

  • schema (dict)

  • strict (bool | None)

name: str

The name of the response format.

description: str | None

A description of what the response format is for, used by the model to determine how to respond in the format.

schema_param: dict

The schema for the response format, described as a JSON Schema object.

strict: bool | None

Whether to enable strict schema adherence when generating the output

If set to true, the model will follow the exact schema defined in the schema field. Only a subset of JSON Schema is supported when strict is true. Learn more about Structured Outputs in the [function calling guide](docs/guides/function-calling).

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

type: Literal['text', 'json_object', 'json_schema']

The type of response format being defined.

  • text: The default response format, which can be either text or any

    value needed.

  • json_object: Enables JSON mode, which guarantees the message the model

    generates is valid JSON.

  • json_schema: Enables Structured Outputs which guarantees the model will

    match your supplied JSON schema.

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

json_schema: JsonSchema | None

The JSON schema for the response format.

validate_schema()[source]
Return type:

ResponseFormat

class agentscope_runtime.engine.schemas.oai_llm.ToolChoiceInputFunction(*, name)[source]

Bases: BaseModel

Parameters:

name (str)

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

name: str

The name of the function to call.

class agentscope_runtime.engine.schemas.oai_llm.ToolChoice(*, type, function)[source]

Bases: BaseModel

Parameters:
  • type (str)

  • function (ToolChoiceInputFunction)

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

type: str

The type of the tool. Currently, only function is supported.

function: ToolChoiceInputFunction

The function that the model called.

class agentscope_runtime.engine.schemas.oai_llm.Parameters(*, top_p=None, temperature=None, frequency_penalty=None, presence_penalty=None, max_tokens=None, stop=None, stream=True, stream_options=None, tools=None, tool_choice=None, parallel_tool_calls=False, logit_bias=None, top_logprobs=None, logprobs=None, n=1, seed=None, response_format=ResponseFormat(type='text', json_schema=None))[source]

Bases: BaseModel

General Parameters for LLM

Parameters:
  • top_p (float | None)

  • temperature (float | None)

  • frequency_penalty (float | None)

  • presence_penalty (float | None)

  • max_tokens (int | None)

  • stop (str | None | List[str])

  • stream (bool)

  • stream_options (ChatCompletionStreamOptionsParam | None)

  • tools (List[Tool | Dict] | None)

  • tool_choice (str | ToolChoice | None)

  • parallel_tool_calls (bool)

  • logit_bias (Dict[str, int] | None)

  • top_logprobs (int | None)

  • logprobs (bool | None)

  • n (Annotated[int | None, Ge(ge=1), Le(le=5)])

  • seed (int | None)

  • response_format (ResponseFormat | str | None)

model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

top_p: float | None

Nucleus sampling, between (0, 1.0], where the model considers the results of the tokens with top_p probability mass.

So 0.1 means only the tokens comprising the top 10% probability mass are considered.

We generally recommend altering this or temperature but not both.

temperature: float | None

What sampling temperature to use, between 0 and 2.

Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.

We generally recommend altering this or top_p but not both.

frequency_penalty: float | None

Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model’s likelihood to repeat the same line verbatim.

presence_penalty: float | None

Number between -2.0 and 2.0.

Positive values penalize new tokens based on whether they appear in the text so far, increasing the model’s likelihood to talk about new topics.

max_tokens: int | None

The maximum number of [tokens](/tokenizer) that can be generated in the chat completion.

The total length of input tokens and generated tokens is limited by the model’s context length.

stop: str | None | List[str]

Up to 4 sequences where the API will stop generating further tokens.

stream: bool

If set, partial message deltas will be sent, like in ChatGPT.

stream_options: ChatCompletionStreamOptionsParam | None

Options for streaming response. Only set this when you set stream: true.

tools: List[Tool | Dict] | None

A list of tools the model may call.

Currently, only functions are supported as a tool. Use this to provide a list of functions the model may generate JSON inputs for.

tool_choice: str | ToolChoice | None

Controls which (if any) tool is called by the model.

parallel_tool_calls: bool

Whether to enable parallel function calling during tool use.

logit_bias: Dict[str, int] | None

Modify the likelihood of specified tokens appearing in the completion.

Accepts a JSON object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token.

top_logprobs: int | None

An integer between 0 and 20 specifying the number of most likely tokens to return at each token position, each with an associated log probability.

logprobs must be set to true if this parameter is used.

logprobs: bool | None

Whether to return log probabilities of the output tokens or not.

If true, returns the log probabilities of each output token returned in the content of message.

n: int | None

How many chat completion choices to generate for each input message.

Note that you will be charged based on the number of generated tokens across all of the choices. Keep n as 1 to minimize costs.

seed: int | None

If specified, system will make a best effort to sample deterministically, such that repeated requests with the same seed and parameters should return the same result.

response_format: ResponseFormat | str | None

An object specifying the format that the model must output.

Setting to { “type”: “json_object” } enables JSON mode, which guarantees the message the model generates is valid JSON.

agentscope_runtime.engine.schemas.oai_llm.create_chat_completion(message, model_name, id='', finish_reason=None)[source]
Parameters:
  • message (OpenAIMessage)

  • model_name (str)

  • id (str)

  • finish_reason (str | None)

Return type:

ChatCompletion

agentscope_runtime.engine.schemas.oai_llm.create_chat_completion_chunk(message, model_name, id='', finish_reason=None)[source]
Parameters:
  • message (OpenAIMessage)

  • model_name (str)

  • id (str)

  • finish_reason (str | None)

Return type:

ChatCompletionChunk

agentscope_runtime.engine.schemas.oai_llm.is_json_string(s)[source]
Parameters:

s (str | Dict | BaseModel | None)

Return type:

bool

Tracing

class agentscope_runtime.engine.tracing.base.TracerHandler[source]

Bases: ABC

Abstract base class for tracer handlers.

abstract on_start(event_name, payload, **kwargs)[source]

Handle the start of a trace event.

Parameters:
  • event_name (str) – The name of event being traced.

  • payload (Dict[str, Any]) – The payload data for the event.

  • **kwargs (Any) – Additional keyword arguments.

Return type:

None

abstract on_end(event_name, start_payload, end_payload, start_time, **kwargs)[source]

Handle the end of a trace event.

Parameters:
  • event_name (str) – The name of event being traced.

  • start_payload (Dict[str, Any]) – The payload data from event start.

  • end_payload (Dict[str, Any]) – The payload data from event end.

  • start_time (float) – The timestamp when the event started.

  • **kwargs (Any) – Additional keyword arguments.

Return type:

None

abstract on_log(message, **kwargs)[source]

Handle a log message during tracing.

Parameters:
  • message (str) – The log message.

  • **kwargs (Any) – Additional keyword arguments.

Return type:

None

abstract on_error(event_name, start_payload, error, start_time, traceback_info, **kwargs)[source]

Handle an error during tracing.

Parameters:
  • event_name (str) – The type of event being traced.

  • start_payload (Dict[str, Any]) – The payload data from event start.

  • error (Exception) – The exception that occurred.

  • start_time (float) – The timestamp when the event started.

  • traceback_info (str) – The traceback information.

  • **kwargs (Any) – Additional keyword arguments.

Return type:

None

class agentscope_runtime.engine.tracing.base.BaseLogHandler[source]

Bases: TracerHandler

Basic log handler implementation using Python’s logging module.

logging = <module 'logging' from '/opt/hostedtoolcache/Python/3.10.19/x64/lib/python3.10/logging/__init__.py'>
logger = <Logger agentscope_runtime.engine.tracing.base (INFO)>
on_start(event_name, payload, **kwargs)[source]

Log the start of a trace event.

Parameters:
  • event_name (str) – The name of event being traced.

  • payload (Dict[str, Any]) – The payload data for the event.

  • **kwargs (Any) – Additional keyword arguments.

Return type:

None

on_end(event_name, start_payload, end_payload, start_time, **kwargs)[source]

Log the end of a trace event.

Parameters:
  • event_name (str) – The name of event being traced.

  • start_payload (Dict[str, Any]) – The payload data from event start.

  • end_payload (Dict[str, Any]) – The payload data from event end.

  • start_time (float) – The timestamp when the event started.

  • **kwargs (Any) – Additional keyword arguments.

Return type:

None

on_log(message, **kwargs)[source]

Log a message during tracing.

Parameters:
  • message (str) – The log message.

  • **kwargs (Any) – Additional keyword arguments.

Return type:

None

on_error(event_name, start_payload, error, start_time, traceback_info, **kwargs)[source]

Log an error during tracing.

Parameters:
  • event_name (str) – The name of event being traced.

  • start_payload (Dict[str, Any]) – The payload data from event start.

  • error (Exception) – The exception that occurred.

  • start_time (float) – The timestamp when the event started.

  • traceback_info (str) – The traceback information.

  • **kwargs (Any) – Additional keyword arguments.

Return type:

None

class agentscope_runtime.engine.tracing.base.Tracer(handlers)[source]

Bases: object

Tracer class for logging events.

Usage example:

with tracer.event(TraceType.LLM, payload) as event:
    event.log("message")
    # ...logic here...
    end_payload = {"xxx": "value"}
    # optional on_end call for additional payload and kwargs
    event.on_end(end_payload, if_success=True)
Parameters:

handlers (List[TracerHandler])

__init__(handlers)[source]

Initialize the tracer with a list of handlers.

Parameters:
  • handlers (List[TracerHandler]) – List of handlers to process trace

  • events.

event(span, event_name, payload, **kwargs)[source]

Create a context manager for tracing an event.

Parameters:
  • span (Any) – span of event

  • event_name (str) – The name of event being traced.

  • payload (Dict[str, Any]) – The payload data for the event.

  • **kwargs (Any) – Additional keyword arguments.

Yields:

EventContext – The event context for logging and managing the trace.

Return type:

Any

log(message, **kwargs)[source]

Log a message using all registered handlers.

Parameters:
  • message (str) – The log message.

  • **kwargs (Any) – Additional keyword arguments.

Return type:

None

class agentscope_runtime.engine.tracing.base.EventContext(span, handlers, event_name, start_time, start_payload)[source]

Bases: object

Context manager for individual trace events.

Parameters:
__init__(span, handlers, event_name, start_time, start_payload)[source]

Initialize the event context.

Parameters:
  • handlers (List[TracerHandler]) – List of handlers to process

  • events. (trace)

  • event_name (str) – The name of event being traced.

  • start_time (float) – The timestamp when the event started.

  • start_payload (Dict[str, Any]) – The payload data from event start.

  • span (Any)

Return type:

None

on_end(payload, **kwargs)[source]

Set the end payload and additional kwargs for the event.

Parameters:
  • payload (Dict[str, Any]) – The payload data for event end.

  • **kwargs (Any) – Additional keyword arguments.

Return type:

None

on_log(message, **kwargs)[source]

Log a message within this event context.

Parameters:
  • message (str) – The log message.

  • **kwargs (Any) – Additional keyword arguments.

Return type:

None

finalize(start_payload=None)[source]

Public method to finalize the event.

Parameters:
  • start_payload (Dict[str, Any], optional) – The payload data from

  • start. (event)

Return type:

None

set_attribute(key, value)[source]

Set attribute for the current span.

Parameters:
  • key (str) – key of attribute

  • value (str) – value of attribute

Return type:

None

get_trace_context()[source]
Return type:

Context

class agentscope_runtime.engine.tracing.tracing_util.TracingUtil[source]

Bases: object

static set_request_id(value='')[source]

Set request id

Parameters:

value (str)

Return type:

None

static get_request_id(default_value='')[source]

Get request id

Parameters:

default_value (str)

Return type:

str

static set_trace_header(trace_headers)[source]

Set trace headers

Parameters:

trace_headers (dict) – trace headers to set

Return type:

None

static get_trace_header()[source]

Get trace headers

Returns:

trace headers in dict

Return type:

dict

static set_common_attributes(attributes)[source]

Set common attributes by merging with existing ones

Parameters:

attributes (dict) – common attributes to merge

Return type:

None

static get_common_attributes()[source]

Get common attributes

Returns:

common attributes in dict

Return type:

dict

static clear_common_attributes()[source]

Clear common attributes

Return type:

None

agentscope_runtime.engine.tracing.tracing_util.get_global_attributes()[source]

Set global common attributes for tracing.

Return type:

dict

class agentscope_runtime.engine.tracing.tracing_metric.TraceType(value)[source]

Bases: str

Callback manager event types.

Parameters:

value (str)

LLM

Logs for the template and response of LLM calls.

TOOL:Logs for the tool name and its arguments and

the output of tool calls

AGENT_STEP

Logs for the agent loop start and end?

LLM = 'LLM'
TOOL = 'TOOL'
AGENT_STEP = 'AGENT_STEP'
MEMORY = 'MEMORY'
SEARCH = 'SEARCH'
AIGC = 'AIGC'
RAG = 'RAG'
INTENTION = 'INTENTION'
PLUGIN = 'PLUGIN'
FUNCTION_CALL = 'FUNCTION_CALL'
AGENT = 'AGENT'
PLANNING = 'PLANNING'
CHAIN = 'CHAIN'
RETRIEVER = 'RETRIEVER'
RERANKER = 'RERANKER'
EMBEDDING = 'EMBEDDING'
TASK = 'TASK'
GUARDRAIL = 'GUARDRAIL'
REWRITER = 'REWRITER'
OTHER = 'OTHER'
__init__(value)[source]

Initialize a TraceType with a string value.

Parameters:

value (str) – The string value for the trace type.

Raises:

ValueError – If value is not a string.

__str__()[source]

Return the string representation of the trace type.

Returns:

The trace type value.

Return type:

str

__repr__()[source]

Return the detailed string representation of the trace type.

Returns:

The trace type representation in the format TraceType(value).

Return type:

str

classmethod add_type(name, value)[source]

Add a new trace type to the class.

Parameters:
  • name (str) – The name of the new trace type attribute.

  • value (str) – The string value for the new trace type.

Raises:
  • ValueError – If name or value are not strings, or if the name

  • already exists.

Return type:

None

class agentscope_runtime.engine.tracing.wrapper.MineType(value)[source]

Bases: str, Enum

MIME type enumeration for content types.

TEXT = 'text/plain'
JSON = 'application/json'
__format__(format_spec)

Returns format using actual value type unless __str__ has been overridden.

agentscope_runtime.engine.tracing.wrapper.trace(trace_type=None, *, trace_name=None, is_root_span=None, get_finish_reason_func=<function get_finish_reason>, merge_output_func=<function merge_incremental_chunk>)[source]

Decorator for tracing function execution.

Parameters:
  • trace_type (Union[TraceType, str]) – The type of trace event.

  • trace_name (Optional[str]) – The name of trace event.

  • is_root_span (Optional[bool]) – Specify current span as root span

  • get_finish_reason_func (Optional[Callable]) – The function to judge if stopped

  • merge_output_func (Optional[Callable]) – The function to merge outputs

Returns:

The decorated function with tracing capabilities.

Return type:

Any

class agentscope_runtime.engine.tracing.local_logging_handler.LogContext(*, time='', step='', model='', user_id='', task_id='', code='', message='', request_id='', context={}, interval={}, service_id='', service_name='', ds_service_id='', ds_service_name='')[source]

Bases: BaseModel

Pydantic model for log context data.

Parameters:
time: str
step: str
model: str
user_id: str
task_id: str
code: str
message: str
request_id: str
context: Dict
interval: Dict
service_id: str
service_name: str
ds_service_id: str
ds_service_name: str
class Config[source]

Bases: object

extra = 'ignore'
model_config: ClassVar[ConfigDict] = {'extra': 'ignore'}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

class agentscope_runtime.engine.tracing.local_logging_handler.JsonFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)[source]

Bases: Formatter

Custom formatter to output logs in llm chat format.

format(record)[source]

Format a log record as JSON.

Parameters:

record (logging.LogRecord) – The log record to format.

Returns:

The formatted log record as a JSON string.

Return type:

str

class agentscope_runtime.engine.tracing.local_logging_handler.LocalLogHandler(log_level=20, log_file_name=None, log_dir='/home/runner/work/agentscope-runtime/agentscope-runtime/cookbook/logs', max_bytes=1073741824, backup_count=7, enable_console=False, **kwargs)[source]

Bases: TracerHandler

llm chat log handler for structured JSON logging.

Parameters:
  • log_level (int)

  • log_file_name (str | None)

  • log_dir (str)

  • max_bytes (int)

  • backup_count (int)

  • enable_console (bool)

  • kwargs (Any)

__init__(log_level=20, log_file_name=None, log_dir='/home/runner/work/agentscope-runtime/agentscope-runtime/cookbook/logs', max_bytes=1073741824, backup_count=7, enable_console=False, **kwargs)[source]

Initialize the llm chat log handler.

Parameters:
  • log_level (int) – The logging level. Defaults to logging.INFO.

  • log_file_name (Optional[str]) – Prefix for log file names. Defaults to None.

  • log_dir (str) – Directory to save log files. Defaults to “./logs”.

  • max_bytes (int) – Maximum size in bytes for a single log file. Defaults to 1GB.

  • backup_count (int) – Number of log files to keep. Defaults to 7.

  • enable_console (bool) – Whether to enable console logging. Defaults to False.

  • **kwargs (Any) – Additional keyword arguments (unused but kept for compatibility).

Return type:

None

on_start(event_name, payload, **kwargs)[source]

Handle the start of a trace event.

Parameters:
  • event_name (str) – The name of event being traced.

  • payload (Dict[str, Any]) – The payload data for the event.

  • **kwargs (Any) – Additional keyword arguments.

Return type:

None

on_end(event_name, start_payload, end_payload, start_time, **kwargs)[source]

Handle the end of a trace event.

Parameters:
  • event_name (str) – The name of event being traced.

  • start_payload (Dict[str, Any]) – The payload data from event start.

  • end_payload (Any) – The payload data from event end.

  • start_time (float) – The timestamp when the event started.

  • **kwargs (Any) – Additional keyword arguments.

Return type:

None

on_log(message, **kwargs)[source]

Handle a log message during tracing.

Parameters:
  • message (str) – The log message.

  • **kwargs (Any) – Additional keyword arguments including: - step_suffix: Optional suffix for step identification - event_type: The type of event being traced - payload: The payload data - start_time: The timestamp when the event started - start_payload: The payload data from event start

Return type:

None

on_error(event_name, start_payload, error, start_time, traceback_info, **kwargs)[source]

Handle an error during tracing.

Parameters:
  • event_name (str) – The name of event being traced.

  • start_payload (Dict[str, Any]) – The payload data from event start.

  • error (Exception) – The exception that occurred.

  • start_time (float) – The timestamp when the event started.

  • traceback_info (str) – The traceback information.

  • **kwargs (Any) – Additional keyword arguments.

Return type:

None

agentscope_runtime.engine.tracing.message_util.merge_incremental_chunk(responses)[source]

Merge an incremental chunk list to a ChatCompletionChunk.

Parameters:

responses (List[ChatCompletionChunk]) – List of incremental chat completion chunks to merge into a single response.

Returns:

The merged chat completion chunk, or None if the input list is empty.

Return type:

Optional[ChatCompletionChunk]

agentscope_runtime.engine.tracing.message_util.get_finish_reason(response)[source]
Parameters:

response (ChatCompletionChunk)

Return type:

str | None

agentscope_runtime.engine.tracing.message_util.merge_agent_response(responses)[source]

Merge a list of incremental response objects into a single AgentResponse.

Parameters:

responses (List[Union[AgentResponse, Message, TextContent]]) – List of incremental responses to merge into a single response.

Returns:

The merged agent response.

Return type:

AgentResponse

agentscope_runtime.engine.tracing.message_util.get_agent_response_finish_reason(response)[source]

Get the finish reason from a response object.

Parameters:

response (Union[AgentResponse, Message, TextContent]) – The response object

Returns:

The finish reason, or None if not finished

Return type:

Optional[str]

agentscope_runtime.engine.tracing.message_util.merge_agent_message(messages)[source]

Merge a list of incremental message objects into a single Message.

Parameters:

messages (List[Union[Message, TextContent]]) – List of incremental messages to merge into a single message.

Returns:

The merged message.

Return type:

Message

agentscope_runtime.engine.tracing.message_util.get_agent_message_finish_reason(message)[source]
Parameters:

message (Message | TextContent | None)

Return type:

str | None

async agentscope_runtime.engine.tracing.asyncio_util.aenumerate(asequence, start=0)[source]

Asynchronously enumerate an async iterator from a given start value.

Parameters:
  • asequence (AsyncIterable[T_co]) – The async iterable to enumerate.

  • start (int) – The starting value for enumeration. Defaults to 0.

Yields:

Tuple[int, T_co] – A tuple containing the index and the item from the async iterable.

Return type:

AsyncIterator[Tuple[int, T_co]]