openai/openai-python
Publicmirrored from https://github.com/openai/openai-pythonAvailable
tests/api_resources/beta/test_threads.py
321lines · modecode
| 1 | # File generated from our OpenAPI spec by Stainless. |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import os |
| 6 | |
| 7 | import pytest |
| 8 | |
| 9 | from openai import OpenAI, AsyncOpenAI |
| 10 | from tests.utils import assert_matches_type |
| 11 | from openai._client import OpenAI, AsyncOpenAI |
| 12 | from openai.types.beta import ( |
| 13 | Thread, |
| 14 | ThreadDeleted, |
| 15 | ) |
| 16 | from openai.types.beta.threads import Run |
| 17 | |
| 18 | base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") |
| 19 | api_key = "My API Key" |
| 20 | |
| 21 | |
| 22 | class TestThreads: |
| 23 | strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 24 | loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 25 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 26 | |
| 27 | @parametrize |
| 28 | def test_method_create(self, client: OpenAI) -> None: |
| 29 | thread = client.beta.threads.create() |
| 30 | assert_matches_type(Thread, thread, path=["response"]) |
| 31 | |
| 32 | @parametrize |
| 33 | def test_method_create_with_all_params(self, client: OpenAI) -> None: |
| 34 | thread = client.beta.threads.create( |
| 35 | messages=[ |
| 36 | { |
| 37 | "role": "user", |
| 38 | "content": "x", |
| 39 | "file_ids": ["string"], |
| 40 | "metadata": {}, |
| 41 | }, |
| 42 | { |
| 43 | "role": "user", |
| 44 | "content": "x", |
| 45 | "file_ids": ["string"], |
| 46 | "metadata": {}, |
| 47 | }, |
| 48 | { |
| 49 | "role": "user", |
| 50 | "content": "x", |
| 51 | "file_ids": ["string"], |
| 52 | "metadata": {}, |
| 53 | }, |
| 54 | ], |
| 55 | metadata={}, |
| 56 | ) |
| 57 | assert_matches_type(Thread, thread, path=["response"]) |
| 58 | |
| 59 | @parametrize |
| 60 | def test_raw_response_create(self, client: OpenAI) -> None: |
| 61 | response = client.beta.threads.with_raw_response.create() |
| 62 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 63 | thread = response.parse() |
| 64 | assert_matches_type(Thread, thread, path=["response"]) |
| 65 | |
| 66 | @parametrize |
| 67 | def test_method_retrieve(self, client: OpenAI) -> None: |
| 68 | thread = client.beta.threads.retrieve( |
| 69 | "string", |
| 70 | ) |
| 71 | assert_matches_type(Thread, thread, path=["response"]) |
| 72 | |
| 73 | @parametrize |
| 74 | def test_raw_response_retrieve(self, client: OpenAI) -> None: |
| 75 | response = client.beta.threads.with_raw_response.retrieve( |
| 76 | "string", |
| 77 | ) |
| 78 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 79 | thread = response.parse() |
| 80 | assert_matches_type(Thread, thread, path=["response"]) |
| 81 | |
| 82 | @parametrize |
| 83 | def test_method_update(self, client: OpenAI) -> None: |
| 84 | thread = client.beta.threads.update( |
| 85 | "string", |
| 86 | ) |
| 87 | assert_matches_type(Thread, thread, path=["response"]) |
| 88 | |
| 89 | @parametrize |
| 90 | def test_method_update_with_all_params(self, client: OpenAI) -> None: |
| 91 | thread = client.beta.threads.update( |
| 92 | "string", |
| 93 | metadata={}, |
| 94 | ) |
| 95 | assert_matches_type(Thread, thread, path=["response"]) |
| 96 | |
| 97 | @parametrize |
| 98 | def test_raw_response_update(self, client: OpenAI) -> None: |
| 99 | response = client.beta.threads.with_raw_response.update( |
| 100 | "string", |
| 101 | ) |
| 102 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 103 | thread = response.parse() |
| 104 | assert_matches_type(Thread, thread, path=["response"]) |
| 105 | |
| 106 | @parametrize |
| 107 | def test_method_delete(self, client: OpenAI) -> None: |
| 108 | thread = client.beta.threads.delete( |
| 109 | "string", |
| 110 | ) |
| 111 | assert_matches_type(ThreadDeleted, thread, path=["response"]) |
| 112 | |
| 113 | @parametrize |
| 114 | def test_raw_response_delete(self, client: OpenAI) -> None: |
| 115 | response = client.beta.threads.with_raw_response.delete( |
| 116 | "string", |
| 117 | ) |
| 118 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 119 | thread = response.parse() |
| 120 | assert_matches_type(ThreadDeleted, thread, path=["response"]) |
| 121 | |
| 122 | @parametrize |
| 123 | def test_method_create_and_run(self, client: OpenAI) -> None: |
| 124 | thread = client.beta.threads.create_and_run( |
| 125 | assistant_id="string", |
| 126 | ) |
| 127 | assert_matches_type(Run, thread, path=["response"]) |
| 128 | |
| 129 | @parametrize |
| 130 | def test_method_create_and_run_with_all_params(self, client: OpenAI) -> None: |
| 131 | thread = client.beta.threads.create_and_run( |
| 132 | assistant_id="string", |
| 133 | instructions="string", |
| 134 | metadata={}, |
| 135 | model="string", |
| 136 | thread={ |
| 137 | "messages": [ |
| 138 | { |
| 139 | "role": "user", |
| 140 | "content": "x", |
| 141 | "file_ids": ["string"], |
| 142 | "metadata": {}, |
| 143 | }, |
| 144 | { |
| 145 | "role": "user", |
| 146 | "content": "x", |
| 147 | "file_ids": ["string"], |
| 148 | "metadata": {}, |
| 149 | }, |
| 150 | { |
| 151 | "role": "user", |
| 152 | "content": "x", |
| 153 | "file_ids": ["string"], |
| 154 | "metadata": {}, |
| 155 | }, |
| 156 | ], |
| 157 | "metadata": {}, |
| 158 | }, |
| 159 | tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}], |
| 160 | ) |
| 161 | assert_matches_type(Run, thread, path=["response"]) |
| 162 | |
| 163 | @parametrize |
| 164 | def test_raw_response_create_and_run(self, client: OpenAI) -> None: |
| 165 | response = client.beta.threads.with_raw_response.create_and_run( |
| 166 | assistant_id="string", |
| 167 | ) |
| 168 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 169 | thread = response.parse() |
| 170 | assert_matches_type(Run, thread, path=["response"]) |
| 171 | |
| 172 | |
| 173 | class TestAsyncThreads: |
| 174 | strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True) |
| 175 | loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False) |
| 176 | parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"]) |
| 177 | |
| 178 | @parametrize |
| 179 | async def test_method_create(self, client: AsyncOpenAI) -> None: |
| 180 | thread = await client.beta.threads.create() |
| 181 | assert_matches_type(Thread, thread, path=["response"]) |
| 182 | |
| 183 | @parametrize |
| 184 | async def test_method_create_with_all_params(self, client: AsyncOpenAI) -> None: |
| 185 | thread = await client.beta.threads.create( |
| 186 | messages=[ |
| 187 | { |
| 188 | "role": "user", |
| 189 | "content": "x", |
| 190 | "file_ids": ["string"], |
| 191 | "metadata": {}, |
| 192 | }, |
| 193 | { |
| 194 | "role": "user", |
| 195 | "content": "x", |
| 196 | "file_ids": ["string"], |
| 197 | "metadata": {}, |
| 198 | }, |
| 199 | { |
| 200 | "role": "user", |
| 201 | "content": "x", |
| 202 | "file_ids": ["string"], |
| 203 | "metadata": {}, |
| 204 | }, |
| 205 | ], |
| 206 | metadata={}, |
| 207 | ) |
| 208 | assert_matches_type(Thread, thread, path=["response"]) |
| 209 | |
| 210 | @parametrize |
| 211 | async def test_raw_response_create(self, client: AsyncOpenAI) -> None: |
| 212 | response = await client.beta.threads.with_raw_response.create() |
| 213 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 214 | thread = response.parse() |
| 215 | assert_matches_type(Thread, thread, path=["response"]) |
| 216 | |
| 217 | @parametrize |
| 218 | async def test_method_retrieve(self, client: AsyncOpenAI) -> None: |
| 219 | thread = await client.beta.threads.retrieve( |
| 220 | "string", |
| 221 | ) |
| 222 | assert_matches_type(Thread, thread, path=["response"]) |
| 223 | |
| 224 | @parametrize |
| 225 | async def test_raw_response_retrieve(self, client: AsyncOpenAI) -> None: |
| 226 | response = await client.beta.threads.with_raw_response.retrieve( |
| 227 | "string", |
| 228 | ) |
| 229 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 230 | thread = response.parse() |
| 231 | assert_matches_type(Thread, thread, path=["response"]) |
| 232 | |
| 233 | @parametrize |
| 234 | async def test_method_update(self, client: AsyncOpenAI) -> None: |
| 235 | thread = await client.beta.threads.update( |
| 236 | "string", |
| 237 | ) |
| 238 | assert_matches_type(Thread, thread, path=["response"]) |
| 239 | |
| 240 | @parametrize |
| 241 | async def test_method_update_with_all_params(self, client: AsyncOpenAI) -> None: |
| 242 | thread = await client.beta.threads.update( |
| 243 | "string", |
| 244 | metadata={}, |
| 245 | ) |
| 246 | assert_matches_type(Thread, thread, path=["response"]) |
| 247 | |
| 248 | @parametrize |
| 249 | async def test_raw_response_update(self, client: AsyncOpenAI) -> None: |
| 250 | response = await client.beta.threads.with_raw_response.update( |
| 251 | "string", |
| 252 | ) |
| 253 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 254 | thread = response.parse() |
| 255 | assert_matches_type(Thread, thread, path=["response"]) |
| 256 | |
| 257 | @parametrize |
| 258 | async def test_method_delete(self, client: AsyncOpenAI) -> None: |
| 259 | thread = await client.beta.threads.delete( |
| 260 | "string", |
| 261 | ) |
| 262 | assert_matches_type(ThreadDeleted, thread, path=["response"]) |
| 263 | |
| 264 | @parametrize |
| 265 | async def test_raw_response_delete(self, client: AsyncOpenAI) -> None: |
| 266 | response = await client.beta.threads.with_raw_response.delete( |
| 267 | "string", |
| 268 | ) |
| 269 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 270 | thread = response.parse() |
| 271 | assert_matches_type(ThreadDeleted, thread, path=["response"]) |
| 272 | |
| 273 | @parametrize |
| 274 | async def test_method_create_and_run(self, client: AsyncOpenAI) -> None: |
| 275 | thread = await client.beta.threads.create_and_run( |
| 276 | assistant_id="string", |
| 277 | ) |
| 278 | assert_matches_type(Run, thread, path=["response"]) |
| 279 | |
| 280 | @parametrize |
| 281 | async def test_method_create_and_run_with_all_params(self, client: AsyncOpenAI) -> None: |
| 282 | thread = await client.beta.threads.create_and_run( |
| 283 | assistant_id="string", |
| 284 | instructions="string", |
| 285 | metadata={}, |
| 286 | model="string", |
| 287 | thread={ |
| 288 | "messages": [ |
| 289 | { |
| 290 | "role": "user", |
| 291 | "content": "x", |
| 292 | "file_ids": ["string"], |
| 293 | "metadata": {}, |
| 294 | }, |
| 295 | { |
| 296 | "role": "user", |
| 297 | "content": "x", |
| 298 | "file_ids": ["string"], |
| 299 | "metadata": {}, |
| 300 | }, |
| 301 | { |
| 302 | "role": "user", |
| 303 | "content": "x", |
| 304 | "file_ids": ["string"], |
| 305 | "metadata": {}, |
| 306 | }, |
| 307 | ], |
| 308 | "metadata": {}, |
| 309 | }, |
| 310 | tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}], |
| 311 | ) |
| 312 | assert_matches_type(Run, thread, path=["response"]) |
| 313 | |
| 314 | @parametrize |
| 315 | async def test_raw_response_create_and_run(self, client: AsyncOpenAI) -> None: |
| 316 | response = await client.beta.threads.with_raw_response.create_and_run( |
| 317 | assistant_id="string", |
| 318 | ) |
| 319 | assert response.http_request.headers.get("X-Stainless-Lang") == "python" |
| 320 | thread = response.parse() |
| 321 | assert_matches_type(Run, thread, path=["response"]) |
| 322 | |