openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.13.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/beta/test_threads.py

504lines · 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
a47375b7Stainless Bot2 years ago12from openai.types.beta import (
13Thread,
14ThreadDeleted,
15)
baa9f07fRobert Craigie2 years ago16from openai.types.beta.threads import Run
17
18base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
19
20
21class TestThreads:
98d779fbStainless Bot2 years ago22parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
baa9f07fRobert Craigie2 years ago23
24@parametrize
25def test_method_create(self, client: OpenAI) -> None:
26thread = client.beta.threads.create()
27assert_matches_type(Thread, thread, path=["response"])
28
29@parametrize
30def test_method_create_with_all_params(self, client: OpenAI) -> None:
31thread = client.beta.threads.create(
32messages=[
33{
34"role": "user",
35"content": "x",
36"file_ids": ["string"],
37"metadata": {},
38},
39{
40"role": "user",
41"content": "x",
42"file_ids": ["string"],
43"metadata": {},
44},
45{
46"role": "user",
47"content": "x",
48"file_ids": ["string"],
49"metadata": {},
50},
51],
52metadata={},
53)
54assert_matches_type(Thread, thread, path=["response"])
55
56@parametrize
57def test_raw_response_create(self, client: OpenAI) -> None:
58response = client.beta.threads.with_raw_response.create()
86379b44Stainless Bot2 years ago59
60assert response.is_closed is True
baa9f07fRobert Craigie2 years ago61assert response.http_request.headers.get("X-Stainless-Lang") == "python"
62thread = response.parse()
63assert_matches_type(Thread, thread, path=["response"])
64
86379b44Stainless Bot2 years ago65@parametrize
66def test_streaming_response_create(self, client: OpenAI) -> None:
67with client.beta.threads.with_streaming_response.create() as response:
68assert not response.is_closed
69assert response.http_request.headers.get("X-Stainless-Lang") == "python"
70
71thread = response.parse()
72assert_matches_type(Thread, thread, path=["response"])
73
74assert cast(Any, response.is_closed) is True
75
baa9f07fRobert Craigie2 years ago76@parametrize
77def test_method_retrieve(self, client: OpenAI) -> None:
78thread = client.beta.threads.retrieve(
79"string",
80)
81assert_matches_type(Thread, thread, path=["response"])
82
83@parametrize
84def test_raw_response_retrieve(self, client: OpenAI) -> None:
85response = client.beta.threads.with_raw_response.retrieve(
86"string",
87)
86379b44Stainless Bot2 years ago88
89assert response.is_closed is True
baa9f07fRobert Craigie2 years ago90assert response.http_request.headers.get("X-Stainless-Lang") == "python"
91thread = response.parse()
92assert_matches_type(Thread, thread, path=["response"])
93
86379b44Stainless Bot2 years ago94@parametrize
95def test_streaming_response_retrieve(self, client: OpenAI) -> None:
96with client.beta.threads.with_streaming_response.retrieve(
97"string",
98) as response:
99assert not response.is_closed
100assert response.http_request.headers.get("X-Stainless-Lang") == "python"
101
102thread = response.parse()
103assert_matches_type(Thread, thread, path=["response"])
104
105assert cast(Any, response.is_closed) is True
106
023a4e66Stainless Bot2 years ago107@parametrize
108def test_path_params_retrieve(self, client: OpenAI) -> None:
109with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
110client.beta.threads.with_raw_response.retrieve(
111"",
112)
113
baa9f07fRobert Craigie2 years ago114@parametrize
115def test_method_update(self, client: OpenAI) -> None:
116thread = client.beta.threads.update(
117"string",
118)
119assert_matches_type(Thread, thread, path=["response"])
120
121@parametrize
122def test_method_update_with_all_params(self, client: OpenAI) -> None:
123thread = client.beta.threads.update(
124"string",
125metadata={},
126)
127assert_matches_type(Thread, thread, path=["response"])
128
129@parametrize
130def test_raw_response_update(self, client: OpenAI) -> None:
131response = client.beta.threads.with_raw_response.update(
132"string",
133)
86379b44Stainless Bot2 years ago134
135assert response.is_closed is True
baa9f07fRobert Craigie2 years ago136assert response.http_request.headers.get("X-Stainless-Lang") == "python"
137thread = response.parse()
138assert_matches_type(Thread, thread, path=["response"])
139
86379b44Stainless Bot2 years ago140@parametrize
141def test_streaming_response_update(self, client: OpenAI) -> None:
142with client.beta.threads.with_streaming_response.update(
143"string",
144) as response:
145assert not response.is_closed
146assert response.http_request.headers.get("X-Stainless-Lang") == "python"
147
148thread = response.parse()
149assert_matches_type(Thread, thread, path=["response"])
150
151assert cast(Any, response.is_closed) is True
152
023a4e66Stainless Bot2 years ago153@parametrize
154def test_path_params_update(self, client: OpenAI) -> None:
155with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
156client.beta.threads.with_raw_response.update(
157"",
158)
159
baa9f07fRobert Craigie2 years ago160@parametrize
161def test_method_delete(self, client: OpenAI) -> None:
162thread = client.beta.threads.delete(
163"string",
164)
165assert_matches_type(ThreadDeleted, thread, path=["response"])
166
167@parametrize
168def test_raw_response_delete(self, client: OpenAI) -> None:
169response = client.beta.threads.with_raw_response.delete(
170"string",
171)
86379b44Stainless Bot2 years ago172
173assert response.is_closed is True
baa9f07fRobert Craigie2 years ago174assert response.http_request.headers.get("X-Stainless-Lang") == "python"
175thread = response.parse()
176assert_matches_type(ThreadDeleted, thread, path=["response"])
177
86379b44Stainless Bot2 years ago178@parametrize
179def test_streaming_response_delete(self, client: OpenAI) -> None:
180with client.beta.threads.with_streaming_response.delete(
181"string",
182) as response:
183assert not response.is_closed
184assert response.http_request.headers.get("X-Stainless-Lang") == "python"
185
186thread = response.parse()
187assert_matches_type(ThreadDeleted, thread, path=["response"])
188
189assert cast(Any, response.is_closed) is True
190
023a4e66Stainless Bot2 years ago191@parametrize
192def test_path_params_delete(self, client: OpenAI) -> None:
193with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
194client.beta.threads.with_raw_response.delete(
195"",
196)
197
baa9f07fRobert Craigie2 years ago198@parametrize
199def test_method_create_and_run(self, client: OpenAI) -> None:
200thread = client.beta.threads.create_and_run(
201assistant_id="string",
202)
203assert_matches_type(Run, thread, path=["response"])
204
205@parametrize
206def test_method_create_and_run_with_all_params(self, client: OpenAI) -> None:
207thread = client.beta.threads.create_and_run(
208assistant_id="string",
209instructions="string",
210metadata={},
211model="string",
212thread={
213"messages": [
214{
215"role": "user",
216"content": "x",
217"file_ids": ["string"],
218"metadata": {},
219},
220{
221"role": "user",
222"content": "x",
223"file_ids": ["string"],
224"metadata": {},
225},
226{
227"role": "user",
228"content": "x",
229"file_ids": ["string"],
230"metadata": {},
231},
232],
233"metadata": {},
234},
235tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}],
236)
237assert_matches_type(Run, thread, path=["response"])
238
239@parametrize
240def test_raw_response_create_and_run(self, client: OpenAI) -> None:
241response = client.beta.threads.with_raw_response.create_and_run(
242assistant_id="string",
243)
86379b44Stainless Bot2 years ago244
245assert response.is_closed is True
baa9f07fRobert Craigie2 years ago246assert response.http_request.headers.get("X-Stainless-Lang") == "python"
247thread = response.parse()
248assert_matches_type(Run, thread, path=["response"])
249
86379b44Stainless Bot2 years ago250@parametrize
251def test_streaming_response_create_and_run(self, client: OpenAI) -> None:
252with client.beta.threads.with_streaming_response.create_and_run(
253assistant_id="string",
254) as response:
255assert not response.is_closed
256assert response.http_request.headers.get("X-Stainless-Lang") == "python"
257
258thread = response.parse()
259assert_matches_type(Run, thread, path=["response"])
260
261assert cast(Any, response.is_closed) is True
262
baa9f07fRobert Craigie2 years ago263
264class TestAsyncThreads:
98d779fbStainless Bot2 years ago265parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
baa9f07fRobert Craigie2 years ago266
267@parametrize
98d779fbStainless Bot2 years ago268async def test_method_create(self, async_client: AsyncOpenAI) -> None:
269thread = await async_client.beta.threads.create()
baa9f07fRobert Craigie2 years ago270assert_matches_type(Thread, thread, path=["response"])
271
272@parametrize
98d779fbStainless Bot2 years ago273async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
274thread = await async_client.beta.threads.create(
baa9f07fRobert Craigie2 years ago275messages=[
276{
277"role": "user",
278"content": "x",
279"file_ids": ["string"],
280"metadata": {},
281},
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],
295metadata={},
296)
297assert_matches_type(Thread, thread, path=["response"])
298
299@parametrize
98d779fbStainless Bot2 years ago300async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
301response = await async_client.beta.threads.with_raw_response.create()
86379b44Stainless Bot2 years ago302
303assert response.is_closed is True
baa9f07fRobert Craigie2 years ago304assert response.http_request.headers.get("X-Stainless-Lang") == "python"
305thread = response.parse()
306assert_matches_type(Thread, thread, path=["response"])
307
86379b44Stainless Bot2 years ago308@parametrize
98d779fbStainless Bot2 years ago309async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
310async with async_client.beta.threads.with_streaming_response.create() as response:
86379b44Stainless Bot2 years ago311assert not response.is_closed
312assert response.http_request.headers.get("X-Stainless-Lang") == "python"
313
314thread = await response.parse()
315assert_matches_type(Thread, thread, path=["response"])
316
317assert cast(Any, response.is_closed) is True
318
baa9f07fRobert Craigie2 years ago319@parametrize
98d779fbStainless Bot2 years ago320async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None:
321thread = await async_client.beta.threads.retrieve(
baa9f07fRobert Craigie2 years ago322"string",
323)
324assert_matches_type(Thread, thread, path=["response"])
325
326@parametrize
98d779fbStainless Bot2 years ago327async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None:
328response = await async_client.beta.threads.with_raw_response.retrieve(
baa9f07fRobert Craigie2 years ago329"string",
330)
86379b44Stainless Bot2 years ago331
332assert response.is_closed is True
baa9f07fRobert Craigie2 years ago333assert response.http_request.headers.get("X-Stainless-Lang") == "python"
334thread = response.parse()
335assert_matches_type(Thread, thread, path=["response"])
336
86379b44Stainless Bot2 years ago337@parametrize
98d779fbStainless Bot2 years ago338async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None:
339async with async_client.beta.threads.with_streaming_response.retrieve(
86379b44Stainless Bot2 years ago340"string",
341) as response:
342assert not response.is_closed
343assert response.http_request.headers.get("X-Stainless-Lang") == "python"
344
345thread = await response.parse()
346assert_matches_type(Thread, thread, path=["response"])
347
348assert cast(Any, response.is_closed) is True
349
023a4e66Stainless Bot2 years ago350@parametrize
98d779fbStainless Bot2 years ago351async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None:
023a4e66Stainless Bot2 years ago352with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
98d779fbStainless Bot2 years ago353await async_client.beta.threads.with_raw_response.retrieve(
023a4e66Stainless Bot2 years ago354"",
355)
356
baa9f07fRobert Craigie2 years ago357@parametrize
98d779fbStainless Bot2 years ago358async def test_method_update(self, async_client: AsyncOpenAI) -> None:
359thread = await async_client.beta.threads.update(
baa9f07fRobert Craigie2 years ago360"string",
361)
362assert_matches_type(Thread, thread, path=["response"])
363
364@parametrize
98d779fbStainless Bot2 years ago365async def test_method_update_with_all_params(self, async_client: AsyncOpenAI) -> None:
366thread = await async_client.beta.threads.update(
baa9f07fRobert Craigie2 years ago367"string",
368metadata={},
369)
370assert_matches_type(Thread, thread, path=["response"])
371
372@parametrize
98d779fbStainless Bot2 years ago373async def test_raw_response_update(self, async_client: AsyncOpenAI) -> None:
374response = await async_client.beta.threads.with_raw_response.update(
baa9f07fRobert Craigie2 years ago375"string",
376)
86379b44Stainless Bot2 years ago377
378assert response.is_closed is True
baa9f07fRobert Craigie2 years ago379assert response.http_request.headers.get("X-Stainless-Lang") == "python"
380thread = response.parse()
381assert_matches_type(Thread, thread, path=["response"])
382
86379b44Stainless Bot2 years ago383@parametrize
98d779fbStainless Bot2 years ago384async def test_streaming_response_update(self, async_client: AsyncOpenAI) -> None:
385async with async_client.beta.threads.with_streaming_response.update(
86379b44Stainless Bot2 years ago386"string",
387) as response:
388assert not response.is_closed
389assert response.http_request.headers.get("X-Stainless-Lang") == "python"
390
391thread = await response.parse()
392assert_matches_type(Thread, thread, path=["response"])
393
394assert cast(Any, response.is_closed) is True
395
023a4e66Stainless Bot2 years ago396@parametrize
98d779fbStainless Bot2 years ago397async def test_path_params_update(self, async_client: AsyncOpenAI) -> None:
023a4e66Stainless Bot2 years ago398with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
98d779fbStainless Bot2 years ago399await async_client.beta.threads.with_raw_response.update(
023a4e66Stainless Bot2 years ago400"",
401)
402
baa9f07fRobert Craigie2 years ago403@parametrize
98d779fbStainless Bot2 years ago404async def test_method_delete(self, async_client: AsyncOpenAI) -> None:
405thread = await async_client.beta.threads.delete(
baa9f07fRobert Craigie2 years ago406"string",
407)
408assert_matches_type(ThreadDeleted, thread, path=["response"])
409
410@parametrize
98d779fbStainless Bot2 years ago411async def test_raw_response_delete(self, async_client: AsyncOpenAI) -> None:
412response = await async_client.beta.threads.with_raw_response.delete(
baa9f07fRobert Craigie2 years ago413"string",
414)
86379b44Stainless Bot2 years ago415
416assert response.is_closed is True
baa9f07fRobert Craigie2 years ago417assert response.http_request.headers.get("X-Stainless-Lang") == "python"
418thread = response.parse()
419assert_matches_type(ThreadDeleted, thread, path=["response"])
420
86379b44Stainless Bot2 years ago421@parametrize
98d779fbStainless Bot2 years ago422async def test_streaming_response_delete(self, async_client: AsyncOpenAI) -> None:
423async with async_client.beta.threads.with_streaming_response.delete(
86379b44Stainless Bot2 years ago424"string",
425) as response:
426assert not response.is_closed
427assert response.http_request.headers.get("X-Stainless-Lang") == "python"
428
429thread = await response.parse()
430assert_matches_type(ThreadDeleted, thread, path=["response"])
431
432assert cast(Any, response.is_closed) is True
433
023a4e66Stainless Bot2 years ago434@parametrize
98d779fbStainless Bot2 years ago435async def test_path_params_delete(self, async_client: AsyncOpenAI) -> None:
023a4e66Stainless Bot2 years ago436with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
98d779fbStainless Bot2 years ago437await async_client.beta.threads.with_raw_response.delete(
023a4e66Stainless Bot2 years ago438"",
439)
440
baa9f07fRobert Craigie2 years ago441@parametrize
98d779fbStainless Bot2 years ago442async def test_method_create_and_run(self, async_client: AsyncOpenAI) -> None:
443thread = await async_client.beta.threads.create_and_run(
baa9f07fRobert Craigie2 years ago444assistant_id="string",
445)
446assert_matches_type(Run, thread, path=["response"])
447
448@parametrize
98d779fbStainless Bot2 years ago449async def test_method_create_and_run_with_all_params(self, async_client: AsyncOpenAI) -> None:
450thread = await async_client.beta.threads.create_and_run(
baa9f07fRobert Craigie2 years ago451assistant_id="string",
452instructions="string",
453metadata={},
454model="string",
455thread={
456"messages": [
457{
458"role": "user",
459"content": "x",
460"file_ids": ["string"],
461"metadata": {},
462},
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"metadata": {},
477},
478tools=[{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}],
479)
480assert_matches_type(Run, thread, path=["response"])
481
482@parametrize
98d779fbStainless Bot2 years ago483async def test_raw_response_create_and_run(self, async_client: AsyncOpenAI) -> None:
484response = await async_client.beta.threads.with_raw_response.create_and_run(
baa9f07fRobert Craigie2 years ago485assistant_id="string",
486)
86379b44Stainless Bot2 years ago487
488assert response.is_closed is True
baa9f07fRobert Craigie2 years ago489assert response.http_request.headers.get("X-Stainless-Lang") == "python"
490thread = response.parse()
491assert_matches_type(Run, thread, path=["response"])
86379b44Stainless Bot2 years ago492
493@parametrize
98d779fbStainless Bot2 years ago494async def test_streaming_response_create_and_run(self, async_client: AsyncOpenAI) -> None:
495async with async_client.beta.threads.with_streaming_response.create_and_run(
86379b44Stainless Bot2 years ago496assistant_id="string",
497) as response:
498assert not response.is_closed
499assert response.http_request.headers.get("X-Stainless-Lang") == "python"
500
501thread = await response.parse()
502assert_matches_type(Run, thread, path=["response"])
503
504assert cast(Any, response.is_closed) is True