Fix KeyError in convert_to_openai_messages for incomplete messages#485
Fix KeyError in convert_to_openai_messages for incomplete messages#485Mr-Neutr0n wants to merge 1 commit into
Conversation
…sages When AgentOps captures incomplete message objects (missing "role" or "tool_calls" keys), convert_to_openai_messages() would crash with KeyError on msg["role"] or call["id"]. This change adds defensive key checking: - Infer role="assistant" when "role" is absent but "tool_calls" exists; skip messages missing both keys entirely - Filter out tool_call entries missing "id", "name", or "arguments" - Guard completion tool entries against missing nested "call" dict keys - Fall back to content-only assistant message when all tool entries are filtered out Fixes microsoft#311
|
Friendly bump! Let me know if there's anything I should update or improve to help move this forward. |
| messages.append( | ||
| ChatCompletionAssistantMessageParam(role="assistant", content=None, tool_calls=tool_calls) | ||
| ) | ||
| if tool_calls: |
There was a problem hiding this comment.
so the message will be skipped if tool calls is empty?
|
@ultmaster — yes, in the If you'd rather the message survive with a content-only assistant message in the tool_calls case (matching the tools branch's fallback), say the word and I'll flip it — small change. |
Summary
convert_to_openai_messages()to handle incomplete message objects captured by AgentOps"role"but has"tool_calls", inferrole="assistant"instead of crashing; skip messages missing both keys"id","name","arguments") rather than raisingKeyError"call"dict or its required"id"/"type"keysFixes #311
Test plan
test_trace_messages_adapter_builds_expected_conversations,test_trace_messages_adapter_handles_multiple_tool_calls)test_convert_to_openai_messages_skips_messages_missing_role-- verifies messages without"role"are skippedtest_convert_to_openai_messages_skips_tool_calls_missing_keys-- verifies incomplete tool_call entries are filteredtest_convert_to_openai_messages_infers_assistant_role_from_tool_calls-- verifies role inference fromtool_callspresence