openai/openai-python

Public

mirrored from https://github.com/openai/openai-pythonAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.8.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/api_resources/beta/test_threads.py

510lines · modeblame

baa9f07fRobert Craigie2 years ago1# File generated from our OpenAPI spec by Stainless.
2
3from __future__ import annotations
4
5import os
86379b44Stainless Bot2 years ago6from typing import Any, cast
baa9f07fRobert Craigie2 years ago7
8import pytest
9
10from openai import OpenAI, AsyncOpenAI
11from tests.utils import assert_matches_type
12from openai._client import OpenAI, AsyncOpenAI
a47375b7Stainless Bot2 years ago13from openai.types.beta import (
14Thread,
15ThreadDeleted,
16)
baa9f07fRobert Craigie2 years ago17from openai.types.beta.threads import Run
18
19base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
20api_key = "My API Key"
21
22
23class TestThreads:
24strict_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
25loose_client = OpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
26parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
27
28@parametrize
29def test_method_create(self, client: OpenAI) -> None:
30thread = client.beta.threads.create()
31assert_matches_type(Thread, thread, path=["response"])
32
33@parametrize
34def test_method_create_with_all_params(self, client: OpenAI) -> None:
35thread = client.beta.threads.create(
36messages=[
37{
38"role": "user",
39"content": "x",
40"file_ids": ["string"],
41"metadata": {},
42},
43{
44"role": "user",
45"content": "x",
46"file_ids": ["string"],
47"metadata": {},
48},
49{
50"role": "user",
51"content": "x",
52"file_ids": ["string"],
53"metadata": {},
54},
55],
56metadata={},
57)
58assert_matches_type(Thread, thread, path=["response"])
59
60@parametrize
61def test_raw_response_create(self, client: OpenAI) -> None:
62response = client.beta.threads.with_raw_response.create()
86379b44Stainless Bot2 years ago63
64assert response.is_closed is True
baa9f07fRobert Craigie2 years ago65assert response.http_request.headers.get("X-Stainless-Lang") == "python"
66thread = response.parse()
67assert_matches_type(Thread, thread, path=["response"])
68
86379b44Stainless Bot2 years ago69@parametrize
70def test_streaming_response_create(self, client: OpenAI) -> None:
71with client.beta.threads.with_streaming_response.create() as response:
72assert not response.is_closed
73assert response.http_request.headers.get("X-Stainless-Lang") == "python"
74
75thread = response.parse()
76assert_matches_type(Thread, thread, path=["response"])
77
78assert cast(Any, response.is_closed) is True
79
baa9f07fRobert Craigie2 years ago80@parametrize
81def test_method_retrieve(self, client: OpenAI) -> None:
82thread = client.beta.threads.retrieve(
83"string",
84)
85assert_matches_type(Thread, thread, path=["response"])
86
87@parametrize
88def test_raw_response_retrieve(self, client: OpenAI) -> None:
89response = client.beta.threads.with_raw_response.retrieve(
90"string",
91)
86379b44Stainless Bot2 years ago92
93assert response.is_closed is True
baa9f07fRobert Craigie2 years ago94assert response.http_request.headers.get("X-Stainless-Lang") == "python"
95thread = response.parse()
96assert_matches_type(Thread, thread, path=["response"])
97
86379b44Stainless Bot2 years ago98@parametrize
99def test_streaming_response_retrieve(self, client: OpenAI) -> None:
100with client.beta.threads.with_streaming_response.retrieve(
101"string",
102) as response:
103assert not response.is_closed
104assert response.http_request.headers.get("X-Stainless-Lang") == "python"
105
106thread = response.parse()
107assert_matches_type(Thread, thread, path=["response"])
108
109assert cast(Any, response.is_closed) is True
110
023a4e66Stainless Bot2 years ago111@parametrize
112def test_path_params_retrieve(self, client: OpenAI) -> None:
113with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
114client.beta.threads.with_raw_response.retrieve(
115"",
116)
117
baa9f07fRobert Craigie2 years ago118@parametrize
119def test_method_update(self, client: OpenAI) -> None:
120thread = client.beta.threads.update(
121"string",
122)
123assert_matches_type(Thread, thread, path=["response"])
124
125@parametrize
126def test_method_update_with_all_params(self, client: OpenAI) -> None:
127thread = client.beta.threads.update(
128"string",
129metadata={},
130)
131assert_matches_type(Thread, thread, path=["response"])
132
133@parametrize
134def test_raw_response_update(self, client: OpenAI) -> None:
135response = client.beta.threads.with_raw_response.update(
136"string",
137)
86379b44Stainless Bot2 years ago138
139assert response.is_closed is True
baa9f07fRobert Craigie2 years ago140assert response.http_request.headers.get("X-Stainless-Lang") == "python"
141thread = response.parse()
142assert_matches_type(Thread, thread, path=["response"])
143
86379b44Stainless Bot2 years ago144@parametrize
145def test_streaming_response_update(self, client: OpenAI) -> None:
146with client.beta.threads.with_streaming_response.update(
147"string",
148) as response:
149assert not response.is_closed
150assert response.http_request.headers.get("X-Stainless-Lang") == "python"
151
152thread = response.parse()
153assert_matches_type(Thread, thread, path=["response"])
154
155assert cast(Any, response.is_closed) is True
156
023a4e66Stainless Bot2 years ago157@parametrize
158def test_path_params_update(self, client: OpenAI) -> None:
159with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
160client.beta.threads.with_raw_response.update(
161"",
162)
163
baa9f07fRobert Craigie2 years ago164@parametrize
165def test_method_delete(self, client: OpenAI) -> None:
166thread = client.beta.threads.delete(
167"string",
168)
169assert_matches_type(ThreadDeleted, thread, path=["response"])
170
171@parametrize
172def test_raw_response_delete(self, client: OpenAI) -> None:
173response = client.beta.threads.with_raw_response.delete(
174"string",
175)
86379b44Stainless Bot2 years ago176
177assert response.is_closed is True
baa9f07fRobert Craigie2 years ago178assert response.http_request.headers.get("X-Stainless-Lang") == "python"
179thread = response.parse()
180assert_matches_type(ThreadDeleted, thread, path=["response"])
181
86379b44Stainless Bot2 years ago182@parametrize
183def test_streaming_response_delete(self, client: OpenAI) -> None:
184with client.beta.threads.with_streaming_response.delete(
185"string",
186) as response:
187assert not response.is_closed
188assert response.http_request.headers.get("X-Stainless-Lang") == "python"
189
190thread = response.parse()
191assert_matches_type(ThreadDeleted, thread, path=["response"])
192
193assert cast(Any, response.is_closed) is True
194
023a4e66Stainless Bot2 years ago195@parametrize
196def test_path_params_delete(self, client: OpenAI) -> None:
197with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
198client.beta.threads.with_raw_response.delete(
199"",
200)
201
baa9f07fRobert Craigie2 years ago202@parametrize
203def test_method_create_and_run(self, client: OpenAI) -> None:
204thread = client.beta.threads.create_and_run(
205assistant_id="string",
206)
207assert_matches_type(Run, thread, path=["response"])
208
209@parametrize
210def test_method_create_and_run_with_all_params(self, client: OpenAI) -> None:
211thread = client.beta.threads.create_and_run(
212assistant_id="string",
213instructions="string",
214metadata={},
215model="string",
216thread={
217"messages": [
218{
219"role": "user",
220"content": "x",
221"file_ids": ["string"],
222"metadata": {},
223},
224{
225"role": "user",
226"content": "x",
227"file_ids": ["string"],
228"metadata": {},
229},
230{
231"role": "user",
232"content": "x",
233"file_ids": ["string"],
234"metadata": {},
235},
236],
237"metadata": {},
238},
239tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}],
240)
241assert_matches_type(Run, thread, path=["response"])
242
243@parametrize
244def test_raw_response_create_and_run(self, client: OpenAI) -> None:
245response = client.beta.threads.with_raw_response.create_and_run(
246assistant_id="string",
247)
86379b44Stainless Bot2 years ago248
249assert response.is_closed is True
baa9f07fRobert Craigie2 years ago250assert response.http_request.headers.get("X-Stainless-Lang") == "python"
251thread = response.parse()
252assert_matches_type(Run, thread, path=["response"])
253
86379b44Stainless Bot2 years ago254@parametrize
255def test_streaming_response_create_and_run(self, client: OpenAI) -> None:
256with client.beta.threads.with_streaming_response.create_and_run(
257assistant_id="string",
258) as response:
259assert not response.is_closed
260assert response.http_request.headers.get("X-Stainless-Lang") == "python"
261
262thread = response.parse()
263assert_matches_type(Run, thread, path=["response"])
264
265assert cast(Any, response.is_closed) is True
266
baa9f07fRobert Craigie2 years ago267
268class TestAsyncThreads:
269strict_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=True)
270loose_client = AsyncOpenAI(base_url=base_url, api_key=api_key, _strict_response_validation=False)
271parametrize = pytest.mark.parametrize("client", [strict_client, loose_client], ids=["strict", "loose"])
272
273@parametrize
274async def test_method_create(self, client: AsyncOpenAI) -> None:
275thread = await client.beta.threads.create()
276assert_matches_type(Thread, thread, path=["response"])
277
278@parametrize
279async def test_method_create_with_all_params(self, client: AsyncOpenAI) -> None:
280thread = await client.beta.threads.create(
281messages=[
282{
283"role": "user",
284"content": "x",
285"file_ids": ["string"],
286"metadata": {},
287},
288{
289"role": "user",
290"content": "x",
291"file_ids": ["string"],
292"metadata": {},
293},
294{
295"role": "user",
296"content": "x",
297"file_ids": ["string"],
298"metadata": {},
299},
300],
301metadata={},
302)
303assert_matches_type(Thread, thread, path=["response"])
304
305@parametrize
306async def test_raw_response_create(self, client: AsyncOpenAI) -> None:
307response = await client.beta.threads.with_raw_response.create()
86379b44Stainless Bot2 years ago308
309assert response.is_closed is True
baa9f07fRobert Craigie2 years ago310assert response.http_request.headers.get("X-Stainless-Lang") == "python"
311thread = response.parse()
312assert_matches_type(Thread, thread, path=["response"])
313
86379b44Stainless Bot2 years ago314@parametrize
315async def test_streaming_response_create(self, client: AsyncOpenAI) -> None:
316async with client.beta.threads.with_streaming_response.create() as response:
317assert not response.is_closed
318assert response.http_request.headers.get("X-Stainless-Lang") == "python"
319
320thread = await response.parse()
321assert_matches_type(Thread, thread, path=["response"])
322
323assert cast(Any, response.is_closed) is True
324
baa9f07fRobert Craigie2 years ago325@parametrize
326async def test_method_retrieve(self, client: AsyncOpenAI) -> None:
327thread = await client.beta.threads.retrieve(
328"string",
329)
330assert_matches_type(Thread, thread, path=["response"])
331
332@parametrize
333async def test_raw_response_retrieve(self, client: AsyncOpenAI) -> None:
334response = await client.beta.threads.with_raw_response.retrieve(
335"string",
336)
86379b44Stainless Bot2 years ago337
338assert response.is_closed is True
baa9f07fRobert Craigie2 years ago339assert response.http_request.headers.get("X-Stainless-Lang") == "python"
340thread = response.parse()
341assert_matches_type(Thread, thread, path=["response"])
342
86379b44Stainless Bot2 years ago343@parametrize
344async def test_streaming_response_retrieve(self, client: AsyncOpenAI) -> None:
345async with client.beta.threads.with_streaming_response.retrieve(
346"string",
347) as response:
348assert not response.is_closed
349assert response.http_request.headers.get("X-Stainless-Lang") == "python"
350
351thread = await response.parse()
352assert_matches_type(Thread, thread, path=["response"])
353
354assert cast(Any, response.is_closed) is True
355
023a4e66Stainless Bot2 years ago356@parametrize
357async def test_path_params_retrieve(self, client: AsyncOpenAI) -> None:
358with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
359await client.beta.threads.with_raw_response.retrieve(
360"",
361)
362
baa9f07fRobert Craigie2 years ago363@parametrize
364async def test_method_update(self, client: AsyncOpenAI) -> None:
365thread = await client.beta.threads.update(
366"string",
367)
368assert_matches_type(Thread, thread, path=["response"])
369
370@parametrize
371async def test_method_update_with_all_params(self, client: AsyncOpenAI) -> None:
372thread = await client.beta.threads.update(
373"string",
374metadata={},
375)
376assert_matches_type(Thread, thread, path=["response"])
377
378@parametrize
379async def test_raw_response_update(self, client: AsyncOpenAI) -> None:
380response = await client.beta.threads.with_raw_response.update(
381"string",
382)
86379b44Stainless Bot2 years ago383
384assert response.is_closed is True
baa9f07fRobert Craigie2 years ago385assert response.http_request.headers.get("X-Stainless-Lang") == "python"
386thread = response.parse()
387assert_matches_type(Thread, thread, path=["response"])
388
86379b44Stainless Bot2 years ago389@parametrize
390async def test_streaming_response_update(self, client: AsyncOpenAI) -> None:
391async with client.beta.threads.with_streaming_response.update(
392"string",
393) as response:
394assert not response.is_closed
395assert response.http_request.headers.get("X-Stainless-Lang") == "python"
396
397thread = await response.parse()
398assert_matches_type(Thread, thread, path=["response"])
399
400assert cast(Any, response.is_closed) is True
401
023a4e66Stainless Bot2 years ago402@parametrize
403async def test_path_params_update(self, client: AsyncOpenAI) -> None:
404with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
405await client.beta.threads.with_raw_response.update(
406"",
407)
408
baa9f07fRobert Craigie2 years ago409@parametrize
410async def test_method_delete(self, client: AsyncOpenAI) -> None:
411thread = await client.beta.threads.delete(
412"string",
413)
414assert_matches_type(ThreadDeleted, thread, path=["response"])
415
416@parametrize
417async def test_raw_response_delete(self, client: AsyncOpenAI) -> None:
418response = await client.beta.threads.with_raw_response.delete(
419"string",
420)
86379b44Stainless Bot2 years ago421
422assert response.is_closed is True
baa9f07fRobert Craigie2 years ago423assert response.http_request.headers.get("X-Stainless-Lang") == "python"
424thread = response.parse()
425assert_matches_type(ThreadDeleted, thread, path=["response"])
426
86379b44Stainless Bot2 years ago427@parametrize
428async def test_streaming_response_delete(self, client: AsyncOpenAI) -> None:
429async with client.beta.threads.with_streaming_response.delete(
430"string",
431) as response:
432assert not response.is_closed
433assert response.http_request.headers.get("X-Stainless-Lang") == "python"
434
435thread = await response.parse()
436assert_matches_type(ThreadDeleted, thread, path=["response"])
437
438assert cast(Any, response.is_closed) is True
439
023a4e66Stainless Bot2 years ago440@parametrize
441async def test_path_params_delete(self, client: AsyncOpenAI) -> None:
442with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
443await client.beta.threads.with_raw_response.delete(
444"",
445)
446
baa9f07fRobert Craigie2 years ago447@parametrize
448async def test_method_create_and_run(self, client: AsyncOpenAI) -> None:
449thread = await client.beta.threads.create_and_run(
450assistant_id="string",
451)
452assert_matches_type(Run, thread, path=["response"])
453
454@parametrize
455async def test_method_create_and_run_with_all_params(self, client: AsyncOpenAI) -> None:
456thread = await client.beta.threads.create_and_run(
457assistant_id="string",
458instructions="string",
459metadata={},
460model="string",
461thread={
462"messages": [
463{
464"role": "user",
465"content": "x",
466"file_ids": ["string"],
467"metadata": {},
468},
469{
470"role": "user",
471"content": "x",
472"file_ids": ["string"],
473"metadata": {},
474},
475{
476"role": "user",
477"content": "x",
478"file_ids": ["string"],
479"metadata": {},
480},
481],
482"metadata": {},
483},
484tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}],
485)
486assert_matches_type(Run, thread, path=["response"])
487
488@parametrize
489async def test_raw_response_create_and_run(self, client: AsyncOpenAI) -> None:
490response = await client.beta.threads.with_raw_response.create_and_run(
491assistant_id="string",
492)
86379b44Stainless Bot2 years ago493
494assert response.is_closed is True
baa9f07fRobert Craigie2 years ago495assert response.http_request.headers.get("X-Stainless-Lang") == "python"
496thread = response.parse()
497assert_matches_type(Run, thread, path=["response"])
86379b44Stainless Bot2 years ago498
499@parametrize
500async def test_streaming_response_create_and_run(self, client: AsyncOpenAI) -> None:
501async with client.beta.threads.with_streaming_response.create_and_run(
502assistant_id="string",
503) as response:
504assert not response.is_closed
505assert response.http_request.headers.get("X-Stainless-Lang") == "python"
506
507thread = await response.parse()
508assert_matches_type(Run, thread, path=["response"])
509
510assert cast(Any, response.is_closed) is True