@@ -114,6 +114,9 @@ class Trainer(TrainerLegacy):
114114 or a dictionary with the initialization parameters for the exporter.
115115 Deprecated. Use [`adapter`][agentlightning.Trainer.adapter] instead."""
116116
117+ port : Optional [int ]
118+ """Port forwarded to [`ClientServerExecutionStrategy`][agentlightning.ClientServerExecutionStrategy]."""
119+
117120 def __init__ (
118121 self ,
119122 * ,
@@ -126,6 +129,7 @@ def __init__(
126129 store : ComponentSpec [LightningStore ] = None ,
127130 runner : ComponentSpec [Runner [Any ]] = None ,
128131 strategy : ComponentSpec [ExecutionStrategy ] = None ,
132+ port : Optional [int ] = None ,
129133 algorithm : ComponentSpec [Algorithm ] = None ,
130134 llm_proxy : ComponentSpec [LLMProxy ] = None ,
131135 n_workers : Optional [int ] = None ,
@@ -139,6 +143,10 @@ def __init__(
139143 Each keyword accepts either a concrete instance, a class, a callable factory, a
140144 registry string, or a lightweight configuration dictionary (see
141145 [`build_component()`][agentlightning.trainer.init_utils.build_component]).
146+
147+ When ``port`` is provided it is forwarded to
148+ [`ClientServerExecutionStrategy`][agentlightning.ClientServerExecutionStrategy]
149+ instances constructed (or supplied) for the trainer.
142150 """
143151 # Do not call super().__init__() here.
144152 # super().__init__() will call TrainerLegacy's initialization, which is not intended.
@@ -209,7 +217,13 @@ def __init__(
209217 self .store = self ._make_store (store )
210218 self .runner = self ._make_runner (runner )
211219
212- self .strategy = self ._make_strategy (strategy , n_runners = self .n_runners )
220+ self .port = port
221+
222+ self .strategy = self ._make_strategy (
223+ strategy ,
224+ n_runners = self .n_runners ,
225+ port = port ,
226+ )
213227 if hasattr (self .strategy , "n_runners" ):
214228 strategy_runners = getattr (self .strategy , "n_runners" )
215229 if isinstance (strategy_runners , int ) and strategy_runners > 0 :
@@ -284,13 +298,20 @@ def _make_strategy(
284298 strategy : ComponentSpec [ExecutionStrategy ],
285299 * ,
286300 n_runners : int ,
301+ port : Optional [int ] = None ,
287302 ) -> ExecutionStrategy :
288303 """Resolve the execution strategy and seed defaults such as `n_runners`."""
289304 if isinstance (strategy , ExecutionStrategy ):
305+ if port is not None and isinstance (strategy , ClientServerExecutionStrategy ):
306+ strategy .server_port = port
290307 return strategy
291308 optional_defaults : Dict [str , Callable [[], Any ]] = {"n_runners" : lambda : n_runners }
309+ if port is not None :
310+ optional_defaults ["server_port" ] = lambda : port
292311
293312 def default_factory () -> ExecutionStrategy :
313+ if port is not None :
314+ return ClientServerExecutionStrategy (n_runners = n_runners , server_port = port )
294315 return ClientServerExecutionStrategy (n_runners = n_runners )
295316
296317 return build_component (
0 commit comments