Skip to content

feat: add transport types + migrate Anthropic normalize path#13073

Closed
kshitijk4poor wants to merge 1 commit into
mainfrom
feat/transport-types
Closed

feat: add transport types + migrate Anthropic normalize path#13073
kshitijk4poor wants to merge 1 commit into
mainfrom
feat/transport-types

Conversation

@kshitijk4poor

Copy link
Copy Markdown
Collaborator

Summary

PR 2 of the provider transport refactor (PR 1: #12975).

Adds shared transport types and migrates one real production path through them.

What ships

  1. agent/transports/types.py — Three dataclasses:

    • NormalizedResponse: content, tool_calls, finish_reason, reasoning (shared), usage, provider_data (opaque)
    • ToolCall: id (str|None), name, arguments, provider_data (per-tool-call protocol metadata)
    • Usage: prompt_tokens, completion_tokens, total_tokens, cached_tokens
    • Two factory helpers: build_tool_call(), map_finish_reason()
  2. normalize_anthropic_response_v2() in anthropic_adapter.py — wraps the existing v1 function and maps output to NormalizedResponse. One call site in run_agent.py (main normalize branch, L11375) uses v2 with a back-compat shim.

Design decisions

Field Shared or provider_data? Why
content, tool_calls, finish_reason Shared Every caller reads these
reasoning Shared Cross-provider (_build_assistant_message L6975-7011). NOT a protocol quirk.
reasoning_details provider_data Opaque blobs consumed only by protocol-specific replay
codex_reasoning_items provider_data Consumed only by Codex input conversion
call_id, response_item_id ToolCall.provider_data Codex replay metadata, per-tool-call
extra_content ToolCall.provider_data Gemini thought_signature, per-tool-call

No ABC, no registry, no base.py, no streaming, no client lifecycle. Those land in PR 3 with AnthropicTransport.

Test plan

  • 46 new tests (all pass):
    • test_types.py: dataclass construction, build_tool_call, map_finish_reason
    • test_anthropic_normalize_v2.py: v1-vs-v2 regression (text, tools, thinking, mixed, stop reasons, mcp prefix, edge cases)
  • 340 anthropic-related tests pass (0 failures)
  • 13,770 total tests pass (75 pre-existing failures, same as main's 86)

Add agent/transports/types.py with three shared dataclasses:
- NormalizedResponse: content, tool_calls, finish_reason, reasoning, usage, provider_data
- ToolCall: id, name, arguments, provider_data (per-tool-call protocol metadata)
- Usage: prompt_tokens, completion_tokens, total_tokens, cached_tokens

Add normalize_anthropic_response_v2() to anthropic_adapter.py — wraps the
existing v1 function and maps its output to NormalizedResponse. One call site
in run_agent.py (the main normalize branch) uses v2 with a back-compat shim
to SimpleNamespace for downstream code.

No ABC, no registry, no streaming, no client lifecycle. Those land in PR 3
with the first concrete transport (AnthropicTransport).

46 new tests:
- test_types.py: dataclass construction, build_tool_call, map_finish_reason
- test_anthropic_normalize_v2.py: v1-vs-v2 regression tests (text, tools,
  thinking, mixed, stop reasons, mcp prefix stripping, edge cases)

Part of the provider transport refactor (PR 2 of 9).
@teknium1

Copy link
Copy Markdown
Contributor

Merged via PR #13347 — cherry-picked onto current main with your authorship preserved in git log (commit 7ab5eeb). Thanks for the clean Step 2 — ships exactly the types + one real migration the refactor plan calls for. On to PR 3 (AnthropicTransport) next.

#13347

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants