test_partial_json_with_invalid_json() — anthropic-sdk-python Function Reference
Architecture documentation for the test_partial_json_with_invalid_json() function in test_partial_json.py from the anthropic-sdk-python codebase.
Entity Profile
Dependency Diagram
graph TD ecee2ef0_fe15_62f5_42bd_1390cd44e956["test_partial_json_with_invalid_json()"] c520095b_d21b_0a1f_51f9_db7cbf763449["TestPartialJson"] ecee2ef0_fe15_62f5_42bd_1390cd44e956 -->|defined in| c520095b_d21b_0a1f_51f9_db7cbf763449 style ecee2ef0_fe15_62f5_42bd_1390cd44e956 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
tests/lib/streaming/test_partial_json.py lines 108–149
def test_partial_json_with_invalid_json(self) -> None:
"""Test that invalid JSON raises an error."""
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),
)
# Invalid JSON input
invalid_json = '{"key": "value", "incomplete_field": bad_value'
event_invalid = BetaRawContentBlockDeltaEvent(
type="content_block_delta",
index=0,
delta=BetaInputJSONDelta(type="input_json_delta", partial_json=invalid_json),
)
# Expect an error when trying to accumulate the invalid JSON
try:
accumulate_event(
event=event_invalid,
current_snapshot=copy.deepcopy(message),
request_headers=httpx.Headers({"anthropic-beta": "fine-grained-tool-streaming-2025-05-14"}),
)
raise AssertionError("Expected ValueError for invalid JSON, but no error was raised.")
except ValueError as e:
assert str(e).startswith(
"Unable to parse tool parameter JSON from model. Please retry your request or adjust your prompt."
)
except Exception as e:
raise AssertionError(f"Unexpected error type: {type(e).__name__} with message: {str(e)}") from e
Domain
Subdomains
Defined In
Source
Frequently Asked Questions
What does test_partial_json_with_invalid_json() do?
test_partial_json_with_invalid_json() is a function in the anthropic-sdk-python codebase, defined in tests/lib/streaming/test_partial_json.py.
Where is test_partial_json_with_invalid_json() defined?
test_partial_json_with_invalid_json() is defined in tests/lib/streaming/test_partial_json.py at line 108.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free