openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/api_resources/responses/test_input_tokens.py
140lines · modecode
| 1 | # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import os |
| 6 | from typing import Any, cast |
| 7 | |
| 8 | import pytest |
| 9 | |
| 10 | from openai import OpenAI, AsyncOpenAI |
| 11 | from tests.utils import assert_matches_type |
| 12 | from openai.types.responses import InputTokenCountResponse |
| 13 | |
| 14 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 15 | |
| 16 | |
| 17 | class TestInputTokens: |
| 18 | parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) |
| 19 | |
| 20 | @parametrize |
| 21 | def test_method_count(self, client: OpenAI) -> None: |
| 22 | input_token = client.responses.input_tokens.count() |
| 23 | assert_matches_type(InputTokenCountResponse, input_token, path=["response"]) |
| 24 | |
| 25 | @parametrize |
| 26 | def test_method_count_with_all_params(self, client: OpenAI) -> None: |
| 27 | input_token = client.responses.input_tokens.count( |
| 28 | conversation="string", |
| 29 | input="string", |
| 30 | instructions="instructions", |
| 31 | model="model", |
| 32 | parallel_tool_calls=True, |
| 33 | previous_response_id="resp_123", |
| 34 | reasoning={ |
| 35 | "effort": "none", |
| 36 | "generate_summary": "auto", |
| 37 | "summary": "auto", |
| 38 | }, |
| 39 | text={ |
| 40 | "format": {"type": "text"}, |
| 41 | "verbosity": "low", |
| 42 | }, |
| 43 | tool_choice="none", |
| 44 | tools=[ |
| 45 | { |
| 46 | "name": "name", |
| 47 | "parameters": {"foo": "bar"}, |
| 48 | "strict": True, |
| 49 | "type": "function", |
| 50 | "defer_loading": True, |
| 51 | "description": "description", |
| 52 | } |
| 53 | ], |
| 54 | truncation="auto", |
| 55 | ) |
| 56 | assert_matches_type(InputTokenCountResponse, input_token, path=["response"]) |
| 57 | |
| 58 | @parametrize |
| 59 | def test_raw_response_count(self, client: OpenAI) -> None: |
| 60 | response = client.responses.input_tokens.with_raw_response.count() |
| 61 | |
| 62 | assert response.is_closed is True |
| 63 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 64 | input_token = response.parse() |
| 65 | assert_matches_type(InputTokenCountResponse, input_token, path=["response"]) |
| 66 | |
| 67 | @parametrize |
| 68 | def test_streaming_response_count(self, client: OpenAI) -> None: |
| 69 | with client.responses.input_tokens.with_streaming_response.count() as response: |
| 70 | assert not response.is_closed |
| 71 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 72 | |
| 73 | input_token = response.parse() |
| 74 | assert_matches_type(InputTokenCountResponse, input_token, path=["response"]) |
| 75 | |
| 76 | assert cast(Any, response.is_closed) is True |
| 77 | |
| 78 | |
| 79 | class TestAsyncInputTokens: |
| 80 | parametrize = pytest.mark.parametrize( |
| 81 | "async_client", [False, True, {"http_client": "aiohttp"}], indirect=True, ids=["loose", "strict", "aiohttp"] |
| 82 | ) |
| 83 | |
| 84 | @parametrize |
| 85 | async def test_method_count(self, async_client: AsyncOpenAI) -> None: |
| 86 | input_token = await async_client.responses.input_tokens.count() |
| 87 | assert_matches_type(InputTokenCountResponse, input_token, path=["response"]) |
| 88 | |
| 89 | @parametrize |
| 90 | async def test_method_count_with_all_params(self, async_client: AsyncOpenAI) -> None: |
| 91 | input_token = await async_client.responses.input_tokens.count( |
| 92 | conversation="string", |
| 93 | input="string", |
| 94 | instructions="instructions", |
| 95 | model="model", |
| 96 | parallel_tool_calls=True, |
| 97 | previous_response_id="resp_123", |
| 98 | reasoning={ |
| 99 | "effort": "none", |
| 100 | "generate_summary": "auto", |
| 101 | "summary": "auto", |
| 102 | }, |
| 103 | text={ |
| 104 | "format": {"type": "text"}, |
| 105 | "verbosity": "low", |
| 106 | }, |
| 107 | tool_choice="none", |
| 108 | tools=[ |
| 109 | { |
| 110 | "name": "name", |
| 111 | "parameters": {"foo": "bar"}, |
| 112 | "strict": True, |
| 113 | "type": "function", |
| 114 | "defer_loading": True, |
| 115 | "description": "description", |
| 116 | } |
| 117 | ], |
| 118 | truncation="auto", |
| 119 | ) |
| 120 | assert_matches_type(InputTokenCountResponse, input_token, path=["response"]) |
| 121 | |
| 122 | @parametrize |
| 123 | async def test_raw_response_count(self, async_client: AsyncOpenAI) -> None: |
| 124 | response = await async_client.responses.input_tokens.with_raw_response.count() |
| 125 | |
| 126 | assert response.is_closed is True |
| 127 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 128 | input_token = response.parse() |
| 129 | assert_matches_type(InputTokenCountResponse, input_token, path=["response"]) |
| 130 | |
| 131 | @parametrize |
| 132 | async def test_streaming_response_count(self, async_client: AsyncOpenAI) -> None: |
| 133 | async with async_client.responses.input_tokens.with_streaming_response.count() as response: |
| 134 | assert not response.is_closed |
| 135 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 136 | |
| 137 | input_token = await response.parse() |
| 138 | assert_matches_type(InputTokenCountResponse, input_token, path=["response"]) |
| 139 | |
| 140 | assert cast(Any, response.is_closed) is True |
| 141 | |