-
Notifications
You must be signed in to change notification settings - Fork 718
fix: deserialize JSON in message function/tool call arguments for experiment run outputs #11299
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
Merged
RogerHYang
merged 1 commit into
main
from
feat/dataset-helpers-deserialize-message-arguments
Feb 12, 2026
Merged
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import json | ||
| from collections.abc import Mapping | ||
| from typing import Any, Literal, Optional | ||
| from typing import Any, Literal, Optional, Sequence, TypedDict | ||
|
|
||
| from openinference.semconv.trace import ( | ||
| MessageAttributes, | ||
|
|
@@ -10,6 +10,7 @@ | |
| ToolAttributes, | ||
| ToolCallAttributes, | ||
| ) | ||
| from typing_extensions import NotRequired | ||
|
|
||
| from phoenix.db.models import Span | ||
| from phoenix.trace.attributes import get_attribute_value | ||
|
|
@@ -193,32 +194,54 @@ def _get_generic_io_value( | |
| return {} | ||
|
|
||
|
|
||
| def _get_message(message: Mapping[str, Any]) -> dict[str, Any]: | ||
| class _Function(TypedDict): | ||
| name: str | ||
| arguments: Any | ||
|
|
||
|
|
||
| class _ToolCall(TypedDict): | ||
| function: _Function | ||
|
|
||
|
|
||
| class _Message(TypedDict): | ||
| role: str | ||
| content: NotRequired[Any] | ||
| name: NotRequired[str] | ||
| function_call: NotRequired[_Function] | ||
| tool_calls: NotRequired[Sequence[_ToolCall]] | ||
|
|
||
|
|
||
| def _get_message(message: Mapping[str, Any]) -> _Message: | ||
| content = get_attribute_value(message, MESSAGE_CONTENT) | ||
| name = get_attribute_value(message, MESSAGE_NAME) | ||
| function_call_name = get_attribute_value(message, MESSAGE_FUNCTION_CALL_NAME) | ||
| function_call_arguments = get_attribute_value(message, MESSAGE_FUNCTION_CALL_ARGUMENTS_JSON) | ||
| function_call = ( | ||
| {"name": function_call_name, "arguments": function_call_arguments} | ||
| if function_call_name is not None or function_call_arguments is not None | ||
| else None | ||
| ) | ||
| tool_calls = [ | ||
| { | ||
| "function": { | ||
| "name": get_attribute_value(tool_call, TOOL_CALL_FUNCTION_NAME), | ||
| "arguments": get_attribute_value(tool_call, TOOL_CALL_FUNCTION_ARGUMENTS_JSON), | ||
| } | ||
| } | ||
| for tool_call in get_attribute_value(message, MESSAGE_TOOL_CALLS) or () | ||
| ] | ||
| return { | ||
| "role": get_attribute_value(message, MESSAGE_ROLE), | ||
| **({"content": content} if content is not None else {}), | ||
| **({"name": name} if name is not None else {}), | ||
| **({"function_call": function_call} if function_call is not None else {}), | ||
| **({"tool_calls": tool_calls} if tool_calls else {}), | ||
| } | ||
| function_call: _Function | None = None | ||
| if function_call_name := get_attribute_value(message, MESSAGE_FUNCTION_CALL_NAME): | ||
| arguments = get_attribute_value(message, MESSAGE_FUNCTION_CALL_ARGUMENTS_JSON) | ||
| function_call_arguments = _safely_json_decode(arguments) | ||
| if function_call_arguments is None: | ||
| function_call_arguments = arguments | ||
| function_call = _Function(name=function_call_name, arguments=function_call_arguments) | ||
| tool_calls = [] | ||
| for tool_call in get_attribute_value(message, MESSAGE_TOOL_CALLS) or (): | ||
| if function_name := get_attribute_value(tool_call, TOOL_CALL_FUNCTION_NAME): | ||
| arguments = get_attribute_value(tool_call, TOOL_CALL_FUNCTION_ARGUMENTS_JSON) | ||
| function_arguments = _safely_json_decode(arguments) | ||
| if function_arguments is None: | ||
| function_arguments = arguments | ||
| function = _Function(name=function_name, arguments=function_arguments) | ||
| tool_calls.append(_ToolCall(function=function)) | ||
|
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. |
||
| role = get_attribute_value(message, MESSAGE_ROLE) or "assistant" | ||
| content = get_attribute_value(message, MESSAGE_CONTENT) | ||
| msg = _Message(role=role) | ||
|
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. |
||
| if content is not None: | ||
| msg["content"] = content | ||
| if name is not None: | ||
| msg["name"] = name | ||
| if function_call is not None: | ||
| msg["function_call"] = function_call | ||
| if tool_calls: | ||
| msg["tool_calls"] = tool_calls | ||
| return msg | ||
|
|
||
|
|
||
| def _parse_retrieval_documents(retrieval_documents: Any) -> Optional[list[dict[str, Any]]]: | ||
|
|
||
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
Oops, something went wrong.
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.
JSON null arguments stay as strings
Low Severity
_get_messagetreats a decodedNoneas a parse failure and falls back to the original string. Because_safely_json_decode("null")returnsNone, valid JSONnullinMESSAGE_FUNCTION_CALL_ARGUMENTS_JSONorTOOL_CALL_FUNCTION_ARGUMENTS_JSONis emitted as"null"instead ofNone.Additional Locations (1)
src/phoenix/server/api/helpers/dataset_helpers.py#L227-L230