From c7ff99d9f5c8f2afdb895574eb234efddde8d1c0 Mon Sep 17 00:00:00 2001 From: Caleb Fahlgren Date: Mon, 16 Nov 2026 13:33:44 -0500 Subject: [PATCH] support sts session traces in agent traces detection --- setup.py | 2 +- src/datasets/packaged_modules/json/json.py | 28 ++++++++++ tests/packaged_modules/test_json.py | 61 ++++++++++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 7b0b89c9cf6..9d4f019b197 100644 --- a/setup.py +++ b/setup.py @@ -199,7 +199,7 @@ "torchcodec>=0.7.0; python_version < '3.14'", # minium version to get windows support, torchcodec doesn't have wheels for 3.14 yet "nibabel>=5.3.1", "trimesh>=4.10.0", - "teich==0.1.5", + "teich==0.3.8", ] NUMPY2_INCOMPATIBLE_LIBRARIES = [ diff --git a/src/datasets/packaged_modules/json/json.py b/src/datasets/packaged_modules/json/json.py index e3e9be7481c..8b19afce62c 100644 --- a/src/datasets/packaged_modules/json/json.py +++ b/src/datasets/packaged_modules/json/json.py @@ -199,6 +199,10 @@ def _generate_tables(self, base_files, files_iterables, original_files, allow_fu harness, session_id, prompt, sent_at, num_user_messages, num_tool_calls = ( parse_traces_info(lines) ) + # STS headers look like pi's "session" event, so rely on teich's detection + # https://huggingface.co/docs/hub/session-traces-format + if training_example["metadata"]["trace_type"] == "sts": + harness = "sts" bonus_fields = { "harness": harness, "session_id": session_id, @@ -414,6 +418,16 @@ def _generate_tables(self, base_files, files_iterables, original_files, allow_fu "cwd": lambda f: f == Value("string"), } ), + # https://huggingface.co/docs/hub/session-traces-format + # uniform {role, content} messages (e.g. no tool calls) are inferred as a struct instead of Json + "sts": datasets.Features( + { + "type": lambda f: f == Value("string"), + "harness": lambda f: f == Value("string"), + "id": lambda f: f == Value("string"), + "message": lambda f: f == datasets.Json() or (isinstance(f, dict) and "role" in f and "content" in f), + } + ), } AGENT_TRACES_FEATURES = datasets.Features( @@ -528,6 +542,9 @@ def get_tool_call_count(trace_event: dict) -> int: message = trace_event["message"] if message.get("role") != "assistant": return 0 + # STS assistant messages list their tool calls in "toolCalls" + if isinstance(message.get("toolCalls"), list): + return len(message["toolCalls"]) content = message.get("content") if not isinstance(content, list): return 0 @@ -542,6 +559,17 @@ def get_trace_event_timestamp(trace_event: dict) -> Optional[str]: timestamp = trace_event.get("timestamp") if isinstance(timestamp, str): return timestamp + # STS messages carry epoch milliseconds in message.timestamp + message = trace_event.get("message") + if isinstance(message, dict) and isinstance(message.get("timestamp"), (int, float)): + try: + return ( + datetime.fromtimestamp(message["timestamp"] / 1000, tz=timezone.utc) + .isoformat(timespec="milliseconds") + .replace("+00:00", "Z") + ) + except (OverflowError, OSError, ValueError): # out-of-range values in user-uploaded traces + return None return None diff --git a/tests/packaged_modules/test_json.py b/tests/packaged_modules/test_json.py index da35cc0830c..96a2b6faa47 100644 --- a/tests/packaged_modules/test_json.py +++ b/tests/packaged_modules/test_json.py @@ -517,6 +517,29 @@ def assert_agent_traces_output(tmp_path, filename, rows, expected, num_sessions= ] +# https://huggingface.co/docs/hub/session-traces-format +STS_SESSION = [ + {"type": "session", "harness": "chat-ui", "id": "sts-session", "name": "What time is it"}, + {"type": "message", "message": {"role": "user", "content": "what time is it?", "timestamp": 1782918000000}}, + { + "type": "message", + "message": { + "role": "assistant", + "content": "", + "toolCalls": [ + {"id": "t1", "function": {"name": "get_time", "arguments": '{"tz": "UTC"}'}}, + {"id": "t2", "function": {"name": "get_date", "arguments": "{}"}}, + ], + }, + }, + {"type": "message", "message": {"role": "tool", "toolCallId": "t1", "content": "15:00"}}, + {"type": "message", "message": {"role": "tool", "toolCallId": "t2", "content": "2026-07-01"}}, + {"type": "message", "message": {"role": "assistant", "content": "it is 15:00 UTC"}}, + {"type": "message", "message": {"role": "user", "content": "thanks"}}, +] +STS_EXPECTED_AGENT_TRACE_FIELDS = ("sts", "sts-session", "what time is it?", "2026-07-01T15:00:00.000Z", 2, 2) + + def test_config_raises_when_invalid_name() -> None: with pytest.raises(InvalidConfigName, match="Bad characters"): _ = JsonConfig(name="name-with-*-invalid-character") @@ -803,6 +826,20 @@ def test_json_leading_null_in_list_recovery_real_data(tmp_path): ("droid", "droid-session", "Inspect the project", "2026-06-02T18:55:30.274Z", 1, 1), id="droid", ), + pytest.param("sts.jsonl", STS_SESSION, STS_EXPECTED_AGENT_TRACE_FIELDS, id="sts"), + pytest.param( + "pi_with_harness.jsonl", + [ + {"type": "session", "id": "pi-session", "harness": "my-agent"}, + { + "timestamp": "2026-04-03T10:01:00.000Z", + "type": "message", + "message": {"role": "user", "content": [{"type": "text", "text": "pi prompt"}]}, + }, + ], + ("pi", "pi-session", "pi prompt", "2026-04-03T10:01:00.000Z", 1, 0), + id="pi-with-harness", + ), pytest.param( "missing_prompt.jsonl", [ @@ -837,6 +874,27 @@ def test_json_generate_tables_with_agent_trace_metadata(tmp_path, filename, rows ("droid", "droid-session", "Inspect the project", "2026-06-02T18:55:30.274Z", 1, 1), id="droid", ), + pytest.param("sts.jsonl", STS_SESSION, STS_EXPECTED_AGENT_TRACE_FIELDS, id="sts"), + pytest.param( + "sts_without_tools.jsonl", + [ + {"type": "session", "harness": "trackio", "id": "sts-session"}, + {"type": "message", "message": {"role": "user", "content": "reproduce the paper"}}, + {"type": "message", "message": {"role": "assistant", "content": "done"}}, + ], + ("sts", "sts-session", "reproduce the paper", None, 1, 0), + id="sts-without-tools", + ), + pytest.param( + "sts_float_timestamp.jsonl", + [ + STS_SESSION[0], + {"type": "message", "message": {**STS_SESSION[1]["message"], "timestamp": 1782918000000.0}}, + ] + + STS_SESSION[2:], + STS_EXPECTED_AGENT_TRACE_FIELDS, + id="sts-float-timestamp", + ), ], ) def test_json_load_dataset_with_agent_trace_metadata(tmp_path, filename, rows, expected): @@ -863,6 +921,9 @@ def test_json_load_dataset_with_agent_trace_metadata(tmp_path, filename, rows, e if filename == "droid.jsonl": assert row["metadata"]["trace_type"] == "droid" assert json.loads(row["trace"].splitlines()[0])["type"] == "session_start" + if filename == "sts.jsonl": + assert row["metadata"]["harness"] == "chat-ui" + assert [len(message.get("tool_calls") or []) for message in row["messages"]] == [0, 2, 0, 0, 0, 0] def test_json_load_dataset_without_droid_marker_stays_ordinary_json(tmp_path):