Home / Class/ TestPartialJson Class — anthropic-sdk-python Architecture

TestPartialJson Class — anthropic-sdk-python Architecture

Architecture documentation for the TestPartialJson class in test_partial_json.py from the anthropic-sdk-python codebase.

Entity Profile

Dependency Diagram

graph TD
  c520095b_d21b_0a1f_51f9_db7cbf763449["TestPartialJson"]
  c99fded7_4bd3_789e_0b8b_6389fef4f668["ToolUseBlock"]
  c520095b_d21b_0a1f_51f9_db7cbf763449 -->|extends| c99fded7_4bd3_789e_0b8b_6389fef4f668
  06ef3a78_b4c0_f7a7_8dcb_f5667ebd7320["test_partial_json.py"]
  c520095b_d21b_0a1f_51f9_db7cbf763449 -->|defined in| 06ef3a78_b4c0_f7a7_8dcb_f5667ebd7320
  9bb433d5_32a1_98ef_213f_282d61ed1f22["test_trailing_strings_mode_header()"]
  c520095b_d21b_0a1f_51f9_db7cbf763449 -->|method| 9bb433d5_32a1_98ef_213f_282d61ed1f22
  ecee2ef0_fe15_62f5_42bd_1390cd44e956["test_partial_json_with_invalid_json()"]
  c520095b_d21b_0a1f_51f9_db7cbf763449 -->|method| ecee2ef0_fe15_62f5_42bd_1390cd44e956

Relationship Graph

Source Code

tests/lib/streaming/test_partial_json.py lines 13–149

class TestPartialJson:
    def test_trailing_strings_mode_header(self) -> None:
        """Test behavior differences with and without the beta header for JSON parsing."""
        message = ParsedBetaMessage(
            id="msg_123",
            type="message",
            role="assistant",
            content=[
                BetaToolUseBlock(
                    type="tool_use",
                    input={},
                    id="tool_123",
                    name="test_tool",
                    caller=BetaDirectCaller(type="direct"),
                )
            ],
            model="claude-sonnet-4-5",
            stop_reason=None,
            stop_sequence=None,
            usage=BetaUsage(input_tokens=10, output_tokens=10),
        )

        # Test case 1: Complete JSON
        complete_json = '{"key": "value"}'
        event_complete = BetaRawContentBlockDeltaEvent(
            type="content_block_delta",
            index=0,
            delta=BetaInputJSONDelta(type="input_json_delta", partial_json=complete_json),
        )

        # Both modes should handle complete JSON the same way
        message1 = accumulate_event(
            event=event_complete,
            current_snapshot=copy.deepcopy(message),
            request_headers=httpx.Headers({"some-header": "value"}),
        )
        message2 = accumulate_event(
            event=event_complete,
            current_snapshot=copy.deepcopy(message),
            request_headers=httpx.Headers({"anthropic-beta": "fine-grained-tool-streaming-2025-05-14"}),
        )

        # Both should parse complete JSON correctly
        assert cast(ToolUseBlock, message1.content[0]).input == {"key": "value"}
        assert cast(ToolUseBlock, message2.content[0]).input == {"key": "value"}

        # Test case 2: Incomplete JSON with trailing string that will be treated differently
        # Here we want to create a situation where regular mode and trailing strings mode behave differently
        incomplete_json = '{"items": ["item1", "item2"], "unfinished_field": "incomplete value'
        event_incomplete = BetaRawContentBlockDeltaEvent(
            type="content_block_delta",
            index=0,
            delta=BetaInputJSONDelta(type="input_json_delta", partial_json=incomplete_json),
        )

        # Without beta header (standard mode)
        message_standard = accumulate_event(
            event=event_incomplete,
            current_snapshot=copy.deepcopy(message),
            request_headers=httpx.Headers({"some-header": "value"}),
        )

        # With beta header (trailing strings mode)
        message_trailing = accumulate_event(
            event=event_incomplete,
            current_snapshot=copy.deepcopy(message),
            request_headers=httpx.Headers({"anthropic-beta": "fine-grained-tool-streaming-2025-05-14"}),
        )

        # Get the tool use blocks
        standard_tool = cast(ToolUseBlock, message_standard.content[0])
        trailing_tool = cast(ToolUseBlock, message_trailing.content[0])

        # Both should have the valid complete part of the JSON
        assert isinstance(standard_tool.input, dict)
        assert isinstance(trailing_tool.input, dict)

        standard_input = standard_tool.input  # type: ignore
        trailing_input = trailing_tool.input  # type: ignore

        # The input should have the items array in both cases

Extends

Frequently Asked Questions

What is the TestPartialJson class?
TestPartialJson is a class in the anthropic-sdk-python codebase, defined in tests/lib/streaming/test_partial_json.py.
Where is TestPartialJson defined?
TestPartialJson is defined in tests/lib/streaming/test_partial_json.py at line 13.
What does TestPartialJson extend?
TestPartialJson extends ToolUseBlock.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free