Agent¶
The base agent and its execution context.
agent
¶
The agent programming model for autonomous scientific research.
A Science ADK agent is a Google Agent Development Kit (ADK) agent. Inheriting
from google.adk.agents.BaseAgent means experiments natively integrate with
ADK sessions, runners, sequential/parallel agent composition, and event streams,
while remaining directly executable by deterministic scientific workflows.
Example
Write a single-class scientific agent:
from science_adk import Agent, Port, Ports
class FitOscillator(Agent):
'''Recover oscillator parameters from simulated trajectory.'''
ports: Ports = Ports(
inputs=[Port("trajectory", "dataframe")],
outputs=[Port("period", "float"), Port("residual", "float")],
)
async def execute(self) -> dict[str, Any]:
df = await self.input("trajectory")
await self.log(f"fitting {len(df)} samples")
result = await self.call_tool(
"fit_sinusoid", t=list(df.t), y=list(df.theta)
)
return {"period": result["period"], "residual": result["rmse"]}
Port
dataclass
¶
A single typed input or output port of an agent.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
The unique name of the port on this agent. |
type |
str
|
Data type of the port (must be one of PORT_TYPES). |
description |
str
|
Explanation of what this port carries. |
required |
bool
|
Whether a value is strictly required on this port. |
from_dict
classmethod
¶
Constructs a Port from a dictionary or string shorthand.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any] | str
|
Dictionary containing port specification or string port name. |
required |
Returns:
| Type | Description |
|---|---|
Port
|
A new Port instance. |
Source code in python/src/science_adk/models.py
Ports
dataclass
¶
The complete input and output port surface of an agent.
Attributes:
| Name | Type | Description |
|---|---|---|
inputs |
list[Port]
|
List of declared input ports. |
outputs |
list[Port]
|
List of declared output ports. |
input
¶
output
¶
from_dict
classmethod
¶
Constructs Ports from dictionary data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict[str, Any] | None
|
Dictionary with 'inputs' and 'outputs' lists. |
required |
Returns:
| Type | Description |
|---|---|
Ports
|
A new Ports instance. |
Source code in python/src/science_adk/models.py
to_dict
¶
Serializes the Ports instance to a dictionary.
AgentError
¶
Bases: RuntimeError
Raised when an agent misuses the runtime (bad port, missing input).
Agent
¶
Bases: BaseAgent
Base class for scientific agents built on Google ADK.
Subclasses declare :attr:ports and implement :meth:execute. Everything
else exists to make that method short, reproducible, and verifiable.
Attributes:
| Name | Type | Description |
|---|---|---|
ports |
Ports
|
Typed input and output port declarations. |
params |
dict[str, Any]
|
Static parameters from the experiment workflow definition. |
purpose |
str
|
Description of what this agent does and why. |
kind |
str
|
Primitive role played by this agent (e.g., algorithm, data). |
execute
async
¶
Executes the scientific computation.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A dictionary mapping declared output port names to their computed values. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If the subclass does not implement this method. |
Source code in python/src/science_adk/agent.py
bind
¶
Provides input values directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**inputs
|
Any
|
Keyword arguments representing input port values. |
{}
|
Returns:
| Type | Description |
|---|---|
Agent
|
Self for chaining. |
Source code in python/src/science_adk/agent.py
attach
¶
Attaches execution services (tools, data storage, logging).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
services
|
Any
|
The execution services provider. |
required |
Returns:
| Type | Description |
|---|---|
Agent
|
Self for chaining. |
Source code in python/src/science_adk/agent.py
input
async
¶
Reads a declared input port.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
port
|
str
|
The name of the input port. |
required |
default
|
Any
|
Default value if the port carries no value and is optional. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The hydrated value associated with the port. |
Raises:
| Type | Description |
|---|---|
AgentError
|
If the port is not declared or a required port has no value. |
Source code in python/src/science_adk/agent.py
param
¶
Reads a static parameter from node configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the parameter. |
required |
default
|
Any
|
Default fallback value if not specified. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The parameter value. |
Source code in python/src/science_adk/agent.py
call_tool
async
¶
Calls a tool by name and records provenance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the tool. |
required |
**arguments
|
Any
|
Arguments to pass to the tool. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
The tool execution result. |
Raises:
| Type | Description |
|---|---|
AgentError
|
If execution services are not attached. |
Source code in python/src/science_adk/agent.py
fetch
async
¶
Fetches a URL with bounded exponential backoff and records provenance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
The URL to fetch. |
required |
timeout
|
int
|
Timeout in seconds. |
60
|
retries
|
int
|
Maximum number of retry attempts. |
3
|
Returns:
| Type | Description |
|---|---|
str
|
The decoded response text. |
Raises:
| Type | Description |
|---|---|
AgentError
|
If the fetch fails after all retry attempts. |
Source code in python/src/science_adk/agent.py
artifact
¶
Reserves a path in this run's data directory and logs file creation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Name of the artifact file. |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to the reserved file location. |
Raises:
| Type | Description |
|---|---|
AgentError
|
If execution services are not attached. |
Source code in python/src/science_adk/agent.py
log
async
¶
Records an informational log message in the provenance trace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Message text to record. |
required |
Source code in python/src/science_adk/agent.py
validate_outputs
¶
Validates execution output against declared output ports.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
outputs
|
Any
|
The output dictionary returned by |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The validated outputs dictionary. |
Raises:
| Type | Description |
|---|---|
AgentError
|
If output types or keys mismatch declared output ports. |
Source code in python/src/science_adk/agent.py
default_ports
classmethod
¶
describe
classmethod
¶
Returns a machine-readable summary of the agent class.
Source code in python/src/science_adk/agent.py
load_agent_class
¶
Imports a single-class agent file and returns its Agent subclass.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path
|
Path to the agent Python file. |
required |
Returns:
| Type | Description |
|---|---|
type[Agent]
|
The single Agent subclass defined in the file. |
Raises:
| Type | Description |
|---|---|
AgentError
|
If the file does not exist, fails to import, or defines zero or multiple Agent subclasses. |