1818)
1919from temporalio .runtime import Runtime , TelemetryConfig , OpenTelemetryConfig
2020from temporalio .converter import (
21+ PayloadCodec ,
2122 DataConverter ,
2223 JSONTypeConverter ,
2324 AdvancedJSONEncoder ,
@@ -89,16 +90,27 @@ def _validate_interceptors(interceptors: list) -> None:
8990 )
9091
9192
92- async def get_temporal_client (temporal_address : str , metrics_url : str | None = None , plugins : list = []) -> Client :
93+ async def get_temporal_client (
94+ temporal_address : str ,
95+ metrics_url : str | None = None ,
96+ plugins : list = [],
97+ payload_codec : PayloadCodec | None = None ,
98+ ) -> Client :
9399 if plugins != []: # We don't need to validate the plugins if they are empty
94100 _validate_plugins (plugins )
95101
96102 # Check if OpenAI plugin is present - it needs to configure its own data converter
97103 # Lazy import to avoid pulling in opentelemetry.sdk for non-Temporal agents
98104 from temporalio .contrib .openai_agents import OpenAIAgentsPlugin
99- has_openai_plugin = any (
100- isinstance (p , OpenAIAgentsPlugin ) for p in (plugins or [])
101- )
105+
106+ has_openai_plugin = any (isinstance (p , OpenAIAgentsPlugin ) for p in (plugins or []))
107+
108+ if has_openai_plugin and payload_codec is not None :
109+ raise ValueError (
110+ "payload_codec is not supported alongside OpenAIAgentsPlugin: the plugin "
111+ "installs its own data converter and the codec would be silently ignored, "
112+ "leaving payloads unencoded. Remove one or the other."
113+ )
102114
103115 # Build connection kwargs
104116 connect_kwargs = {
@@ -108,7 +120,10 @@ async def get_temporal_client(temporal_address: str, metrics_url: str | None = N
108120
109121 # Only set data_converter if OpenAI plugin is not present
110122 if not has_openai_plugin :
111- connect_kwargs ["data_converter" ] = custom_data_converter
123+ data_converter = custom_data_converter
124+ if payload_codec :
125+ data_converter = dataclasses .replace (data_converter , payload_codec = payload_codec )
126+ connect_kwargs ["data_converter" ] = data_converter
112127
113128 if not metrics_url :
114129 client = await Client .connect (** connect_kwargs )
@@ -129,17 +144,21 @@ def __init__(
129144 plugins : list = [],
130145 interceptors : list = [],
131146 metrics_url : str | None = None ,
147+ payload_codec : PayloadCodec | None = None ,
132148 ):
133149 self .task_queue = task_queue
134150 self .activity_handles = []
135151 self .max_workers = max_workers
136152 self .max_concurrent_activities = max_concurrent_activities
137153 self .health_check_server_running = False
138154 self .healthy = False
139- self .health_check_port = health_check_port if health_check_port is not None else EnvironmentVariables .refresh ().HEALTH_CHECK_PORT
155+ self .health_check_port = (
156+ health_check_port if health_check_port is not None else EnvironmentVariables .refresh ().HEALTH_CHECK_PORT
157+ )
140158 self .plugins = plugins
141159 self .interceptors = interceptors
142160 self .metrics_url = metrics_url
161+ self .payload_codec = payload_codec
143162
144163 @overload
145164 async def run (
@@ -175,6 +194,7 @@ async def run(
175194 temporal_address = os .environ .get ("TEMPORAL_ADDRESS" , "localhost:7233" ),
176195 plugins = self .plugins ,
177196 metrics_url = self .metrics_url ,
197+ payload_codec = self .payload_codec ,
178198 )
179199
180200 # Enable debug mode if AgentEx debug is enabled (disables deadlock detection)
0 commit comments