Skip to content

Commit ddcb12c

Browse files
committed
fix(adk): Always inject headers on execute activity
1 parent 9e8fb83 commit ddcb12c

1 file changed

Lines changed: 18 additions & 27 deletions

File tree

src/agentex/lib/core/temporal/plugins/openai_agents/interceptors/context_interceptor.py

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -85,33 +85,24 @@ def __init__(self, next, payload_converter):
8585
def start_activity(self, input: StartActivityInput) -> workflow.ActivityHandle:
8686
"""Add task_id, trace_id, and parent_span_id to headers when starting model activities."""
8787

88-
# Only add headers for model activity calls (OpenAI and Claude)
89-
activity_name = str(input.activity) if hasattr(input, 'activity') else ""
90-
91-
if ("invoke_model_activity" in activity_name or
92-
"invoke-model-activity" in activity_name or
93-
"run_claude_agent_activity" in activity_name):
94-
# Get task_id, trace_id, and parent_span_id from workflow instance instead of inbound interceptor
95-
try:
96-
workflow_instance = workflow.instance()
97-
task_id = getattr(workflow_instance, '_task_id', None)
98-
trace_id = getattr(workflow_instance, '_trace_id', None)
99-
parent_span_id = getattr(workflow_instance, '_parent_span_id', None)
100-
101-
if task_id and trace_id and parent_span_id:
102-
# Initialize headers if needed
103-
if not input.headers:
104-
input.headers = {}
105-
106-
# Add task_id to headers
107-
input.headers[TASK_ID_HEADER] = self._payload_converter.to_payload(task_id) # type: ignore[index]
108-
input.headers[TRACE_ID_HEADER] = self._payload_converter.to_payload(trace_id) # type: ignore[index]
109-
input.headers[PARENT_SPAN_ID_HEADER] = self._payload_converter.to_payload(parent_span_id) # type: ignore[index]
110-
logger.debug(f"[OutboundInterceptor] Added task_id, trace_id, and parent_span_id to activity headers: {task_id}, {trace_id}, {parent_span_id}")
111-
else:
112-
logger.warning("[OutboundInterceptor] No _task_id, _trace_id, or _parent_span_id found in workflow instance")
113-
except Exception as e:
114-
logger.error(f"[OutboundInterceptor] Failed to get task_id, trace_id, or parent_span_id from workflow instance: {e}")
88+
try:
89+
workflow_instance = workflow.instance()
90+
task_id = getattr(workflow_instance, '_task_id', None)
91+
trace_id = getattr(workflow_instance, '_trace_id', None)
92+
parent_span_id = getattr(workflow_instance, '_parent_span_id', None)
93+
94+
if task_id and trace_id and parent_span_id:
95+
if not input.headers:
96+
input.headers = {}
97+
98+
input.headers[TASK_ID_HEADER] = self._payload_converter.to_payload(task_id) # type: ignore[index]
99+
input.headers[TRACE_ID_HEADER] = self._payload_converter.to_payload(trace_id) # type: ignore[index]
100+
input.headers[PARENT_SPAN_ID_HEADER] = self._payload_converter.to_payload(parent_span_id) # type: ignore[index]
101+
logger.debug(f"[OutboundInterceptor] Added task_id, trace_id, and parent_span_id to activity headers: {task_id}, {trace_id}, {parent_span_id}")
102+
else:
103+
logger.warning("[OutboundInterceptor] No _task_id, _trace_id, or _parent_span_id found in workflow instance")
104+
except Exception as e:
105+
logger.error(f"[OutboundInterceptor] Failed to get task_id, trace_id, or parent_span_id from workflow instance: {e}")
115106

116107
return self.next.start_activity(input)
117108

0 commit comments

Comments
 (0)