-
Notifications
You must be signed in to change notification settings - Fork 3k
A suggestion for #40518 - don't serialize args and simplify code #40597
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
lmolkova
wants to merge
2
commits into
Azure:m-hietala/tool_call_tracing_b9
from
lmolkova:m-hietala/tool_call_tracing_b9
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,10 +53,8 @@ | |
GEN_AI_THREAD_RUN_STATUS, | ||
GEN_AI_USAGE_INPUT_TOKENS, | ||
GEN_AI_USAGE_OUTPUT_TOKENS, | ||
GEN_AI_RUNSTEP_START_TIMESTAMP, | ||
GEN_AI_RUNSTEP_END_TIMESTAMP, | ||
GEN_AI_RUNSTEP_CANCEL_TIMESTAMP, | ||
GEN_AI_RUNSTEP_FAIL_TIMESTAMP, | ||
GEN_AI_RUN_STEP_START_TIMESTAMP, | ||
GEN_AI_RUN_STEP_END_TIMESTAMP, | ||
GEN_AI_RUN_STEP_STATUS, | ||
ERROR_MESSAGE, | ||
OperationName, | ||
|
@@ -292,31 +290,24 @@ def _create_event_attributes( | |
|
||
if created_at: | ||
if isinstance(created_at, datetime): | ||
attrs[GEN_AI_RUNSTEP_START_TIMESTAMP] = created_at.isoformat() | ||
attrs[GEN_AI_RUN_STEP_START_TIMESTAMP] = created_at.isoformat() | ||
else: | ||
# fallback in case int or string gets passed | ||
attrs[GEN_AI_RUNSTEP_START_TIMESTAMP] = str(created_at) | ||
attrs[GEN_AI_RUN_STEP_START_TIMESTAMP] = str(created_at) | ||
|
||
end_timestamp = None | ||
if completed_at: | ||
if isinstance(completed_at, datetime): | ||
attrs[GEN_AI_RUNSTEP_END_TIMESTAMP] = completed_at.isoformat() | ||
else: | ||
# fallback in case int or string gets passed | ||
attrs[GEN_AI_RUNSTEP_END_TIMESTAMP] = str(completed_at) | ||
|
||
if cancelled_at: | ||
if isinstance(cancelled_at, datetime): | ||
attrs[GEN_AI_RUNSTEP_CANCEL_TIMESTAMP] = cancelled_at.isoformat() | ||
else: | ||
# fallback in case int or string gets passed | ||
attrs[GEN_AI_RUNSTEP_CANCEL_TIMESTAMP] = str(cancelled_at) | ||
|
||
if failed_at: | ||
if isinstance(failed_at, datetime): | ||
attrs[GEN_AI_RUNSTEP_FAIL_TIMESTAMP] = failed_at.isoformat() | ||
else: | ||
# fallback in case int or string gets passed | ||
attrs[GEN_AI_RUNSTEP_FAIL_TIMESTAMP] = failed_at.isoformat() | ||
end_timestamp = completed_at | ||
elif cancelled_at: | ||
end_timestamp = cancelled_at | ||
elif failed_at: | ||
end_timestamp = failed_at | ||
|
||
if isinstance(end_timestamp, datetime): | ||
attrs[GEN_AI_RUN_STEP_END_TIMESTAMP] = end_timestamp.isoformat() | ||
else: | ||
# fallback in case int or string gets passed | ||
attrs[GEN_AI_RUN_STEP_END_TIMESTAMP] = str(end_timestamp) | ||
|
||
if run_step_last_error: | ||
attrs[ERROR_MESSAGE] = run_step_last_error.message | ||
|
@@ -356,32 +347,6 @@ def add_thread_message_event( | |
usage=usage, | ||
) | ||
|
||
def _get_tool_details(self, tool: Any) -> Dict[str, Any]: | ||
""" | ||
Extracts tool details from a tool object dynamically and ensures the result is JSON-serializable. | ||
|
||
:param tool: The tool object (e.g., RunStepToolCallDetails). | ||
:return: A dictionary containing the tool details. | ||
""" | ||
tool_details = {} | ||
|
||
# Dynamically extract attributes from the tool object | ||
for attr in dir(tool): | ||
# Skip private or special attributes | ||
if not attr.startswith("_") and not callable(getattr(tool, attr)): | ||
try: | ||
value = getattr(tool, attr) | ||
# Ensure the value is JSON-serializable | ||
if isinstance(value, (str, int, float, bool, list, dict, type(None))): | ||
tool_details[attr] = value | ||
else: | ||
# Convert non-serializable objects to strings | ||
tool_details[attr] = str(value) | ||
except Exception as e: | ||
logging.warning("Error extracting attribute '%s': %s", attr, str(e)) | ||
|
||
return tool_details | ||
|
||
def _process_tool_calls(self, step: RunStep) -> List[Dict[str, Any]]: | ||
""" | ||
Helper method to process tool calls and return a list of tool call dictionaries. | ||
|
@@ -408,41 +373,24 @@ def _process_tool_calls(self, step: RunStep) -> List[Dict[str, Any]]: | |
}, | ||
} | ||
elif isinstance(t, RunStepCodeInterpreterToolCall): | ||
try: | ||
parsed_outputs = json.dumps(t.code_interpreter.outputs) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this results in a huge escaped string, there is no need to dump it. |
||
except Exception as e: | ||
logging.warning("Error parsing code interpreter outputs: '%s'", str(e)) | ||
parsed_outputs = {} | ||
|
||
tool_call = { | ||
"id": t.id, | ||
"type": t.type, | ||
"code_interpreter": { | ||
"input": t.code_interpreter.input, | ||
"output": parsed_outputs, | ||
"outputs": [output.as_dict() for output in t.code_interpreter.outputs], | ||
}, | ||
} | ||
elif isinstance(t, RunStepBingGroundingToolCall): | ||
bing_grounding = {} | ||
try: | ||
bing_grounding = json.dumps(t.bing_grounding) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here, dumping results in a string rather than object |
||
except Exception as e: | ||
logging.warning("Error parsing Bing grounding: '%s'", str(e)) | ||
parsed_outputs = {} | ||
|
||
tool_call = { | ||
"id": t.id, | ||
"type": t.type, | ||
t.type: { | ||
"bing_grounding": bing_grounding | ||
"bing_grounding": t.bing_grounding | ||
}, | ||
} | ||
else: | ||
try: | ||
tool_details = json.dumps(self._get_tool_details(t)) | ||
except Exception as e: | ||
logging.warning("Error parsing step details: '%s'", str(e)) | ||
tool_details = "" | ||
tool_details = t.as_dict()[t.type] | ||
|
||
tool_call = { | ||
"id": t.id, | ||
|
@@ -625,10 +573,10 @@ def _add_tool_event_from_thread_run(self, span, run: ThreadRun) -> None: | |
) | ||
|
||
if _trace_agents_content: | ||
attributes[GEN_AI_EVENT_CONTENT] = json.dumps({"tool_calls": tool_calls}) | ||
attributes[GEN_AI_EVENT_CONTENT] = json.dumps({"tool_calls": tool_calls}, ensure_ascii=False) | ||
else: | ||
tool_calls_non_recording = self._remove_function_call_names_and_arguments(tool_calls=tool_calls) | ||
attributes[GEN_AI_EVENT_CONTENT] = json.dumps({"tool_calls": tool_calls_non_recording}) | ||
attributes[GEN_AI_EVENT_CONTENT] = json.dumps({"tool_calls": tool_calls_non_recording}, ensure_ascii=False) | ||
span.span_instance.add_event(name="gen_ai.assistant.message", attributes=attributes) | ||
|
||
def set_end_run(self, span: "AbstractSpan", run: Optional[ThreadRun]) -> None: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we record status of step on line 289, no need to make it part of the attribute name