MCPcopy Create free account
hub / github.com/anthropics/anthropic-sdk-python / session_api

Function session_api

tests/lib/tools/test_open_telemetry.py:2147–2169  ·  view source on GitHub ↗

A client for the session `sesn_1`, whose event history is `history` and whose event stream gives `live`. Reading the history fails when `history` is `None`.

(
    respx_mock: MockRouter,
    spans: Spans,
    *,
    history: list[dict[str, Any]] | None,
    live: list[dict[str, Any]],
    **options: Any,
)

Source from the content-addressed store, hash-verified

2145
2146
2147def session_api(
2148 respx_mock: MockRouter,
2149 spans: Spans,
2150 *,
2151 history: list[dict[str, Any]] | None,
2152 live: list[dict[str, Any]],
2153 **options: Any,
2154) -> AsyncAnthropic:
2155 """A client for the session `sesn_1`, whose event history is `history` and whose event stream gives `live`.
2156
2157 Reading the history fails when `history` is `None`.
2158 """
2159 body = b"".join(f"event: {event['type']}\ndata: {json.dumps(event)}\n\n".encode() for event in live)
2160 respx_mock.get("/v1/sessions/sesn_1/events/stream").mock(
2161 return_value=httpx2.Response(200, content=body, headers={"content-type": "text/event-stream"})
2162 )
2163 respx_mock.get("/v1/sessions/sesn_1/events").mock(
2164 return_value=httpx2.Response(400, json={"type": "error", "error": {"type": "invalid_request_error"}})
2165 if history is None
2166 else httpx2.Response(200, json={"data": history, "next_page": None})
2167 )
2168 respx_mock.post("/v1/sessions/sesn_1/events").mock(return_value=httpx2.Response(200, json={"data": []}))
2169 return AsyncAnthropic(base_url=base_url, api_key="key", open_telemetry=spans.option(**options))
2170
2171
2172class TestSessionToolRunnerSpans:

Calls 4

AsyncAnthropicClass · 0.90
getMethod · 0.45
postMethod · 0.45
optionMethod · 0.45

Tested by

no test coverage detected