Skip to content

Commit 0ddc3d9

Browse files
committed
ensure initial flow state
1 parent 1b1b46c commit 0ddc3d9

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

ddtrace/contrib/internal/crewai/patch.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,15 @@ async def traced_flow_method(crewai, pin, func, instance, args, kwargs):
158158
submit_to_llmobs=True,
159159
flow_instance=instance,
160160
) as span:
161-
initial_flow_state = getattr(instance, "state", {})
161+
flow_state = getattr(instance, "state", {})
162+
initial_flow_state = {}
163+
if isinstance(flow_state, dict):
164+
initial_flow_state = {**flow_state}
165+
elif hasattr(flow_state, "model_dump"):
166+
initial_flow_state = flow_state.model_dump()
162167
result = await func(*args, **kwargs)
163168
kwargs["_dd.instance"] = instance
164-
kwargs["_dd.flow_state"] = initial_flow_state
169+
kwargs["_dd.initial_flow_state"] = initial_flow_state
165170
integration.llmobs_set_tags(span, args=args, kwargs=kwargs, response=result, operation="flow_method")
166171
return result
167172

ddtrace/llmobs/_integrations/crewai.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -280,13 +280,7 @@ def _llmobs_set_tags_flow(self, span, args, kwargs, response):
280280

281281
def _llmobs_set_tags_flow_method(self, span, args, kwargs, response):
282282
flow_instance = kwargs.pop("_dd.instance", None)
283-
state_value = kwargs.pop("_dd.flow_state", {})
284-
initial_flow_state = {}
285-
if isinstance(state_value, dict):
286-
initial_flow_state = state_value
287-
elif hasattr(state_value, "model_dump"):
288-
initial_flow_state = state_value.model_dump()
289-
283+
initial_flow_state = kwargs.pop("_dd.initial_flow_state", {})
290284
input_dict = {
291285
"args": [safe_json(arg) for arg in args[2:]],
292286
"kwargs": {k: safe_json(v) for k, v in kwargs.items()},

0 commit comments

Comments
 (0)