openai/openai-python

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.23.6

Branches

Tags

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

Clone

HTTPS

Download ZIP

tests/api_resources/beta/threads/test_messages.py

473lines · modeblame

5cfb125aStainless Bot2 years ago1# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
baa9f07fRobert Craigie2 years ago2
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.pagination import SyncCursorPage, AsyncCursorPage
e4a9553eStainless Bot2 years ago13from openai.types.beta.threads.message import Message
baa9f07fRobert Craigie2 years ago14
15base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010")
16
17
18class TestMessages:
98d779fbStainless Bot2 years ago19parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"])
baa9f07fRobert Craigie2 years ago20
21@parametrize
22def test_method_create(self, client: OpenAI) -> None:
23message = client.beta.threads.messages.create(
24"string",
25content="x",
26role="user",
27)
5429f696Stainless Bot2 years ago28assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago29
30@parametrize
31def test_method_create_with_all_params(self, client: OpenAI) -> None:
32message = client.beta.threads.messages.create(
33"string",
34content="x",
35role="user",
5b20698dStainless Bot2 years ago36attachments=[
37{
38"file_id": "string",
f73996b7Stainless Bot2 years ago39"tools": [{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}],
5b20698dStainless Bot2 years ago40},
41{
42"file_id": "string",
f73996b7Stainless Bot2 years ago43"tools": [{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}],
5b20698dStainless Bot2 years ago44},
45{
46"file_id": "string",
f73996b7Stainless Bot2 years ago47"tools": [{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}],
5b20698dStainless Bot2 years ago48},
49],
baa9f07fRobert Craigie2 years ago50metadata={},
51)
5429f696Stainless Bot2 years ago52assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago53
54@parametrize
55def test_raw_response_create(self, client: OpenAI) -> None:
56response = client.beta.threads.messages.with_raw_response.create(
57"string",
58content="x",
59role="user",
60)
86379b44Stainless Bot2 years ago61
62assert response.is_closed is True
baa9f07fRobert Craigie2 years ago63assert response.http_request.headers.get("X-Stainless-Lang") == "python"
64message = response.parse()
5429f696Stainless Bot2 years ago65assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago66
86379b44Stainless Bot2 years ago67@parametrize
68def test_streaming_response_create(self, client: OpenAI) -> None:
69with client.beta.threads.messages.with_streaming_response.create(
70"string",
71content="x",
72role="user",
73) as response:
74assert not response.is_closed
75assert response.http_request.headers.get("X-Stainless-Lang") == "python"
76
77message = response.parse()
5429f696Stainless Bot2 years ago78assert_matches_type(Message, message, path=["response"])
86379b44Stainless Bot2 years ago79
80assert cast(Any, response.is_closed) is True
81
023a4e66Stainless Bot2 years ago82@parametrize
83def test_path_params_create(self, client: OpenAI) -> None:
84with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
85client.beta.threads.messages.with_raw_response.create(
86"",
87content="x",
88role="user",
89)
90
baa9f07fRobert Craigie2 years ago91@parametrize
92def test_method_retrieve(self, client: OpenAI) -> None:
93message = client.beta.threads.messages.retrieve(
94"string",
95thread_id="string",
96)
5429f696Stainless Bot2 years ago97assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago98
99@parametrize
100def test_raw_response_retrieve(self, client: OpenAI) -> None:
101response = client.beta.threads.messages.with_raw_response.retrieve(
102"string",
103thread_id="string",
104)
86379b44Stainless Bot2 years ago105
106assert response.is_closed is True
baa9f07fRobert Craigie2 years ago107assert response.http_request.headers.get("X-Stainless-Lang") == "python"
108message = response.parse()
5429f696Stainless Bot2 years ago109assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago110
86379b44Stainless Bot2 years ago111@parametrize
112def test_streaming_response_retrieve(self, client: OpenAI) -> None:
113with client.beta.threads.messages.with_streaming_response.retrieve(
114"string",
115thread_id="string",
116) as response:
117assert not response.is_closed
118assert response.http_request.headers.get("X-Stainless-Lang") == "python"
119
120message = response.parse()
5429f696Stainless Bot2 years ago121assert_matches_type(Message, message, path=["response"])
86379b44Stainless Bot2 years ago122
123assert cast(Any, response.is_closed) is True
124
023a4e66Stainless Bot2 years ago125@parametrize
126def test_path_params_retrieve(self, client: OpenAI) -> None:
127with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
128client.beta.threads.messages.with_raw_response.retrieve(
129"string",
130thread_id="",
131)
132
133with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_id` but received ''"):
134client.beta.threads.messages.with_raw_response.retrieve(
135"",
136thread_id="string",
137)
138
baa9f07fRobert Craigie2 years ago139@parametrize
140def test_method_update(self, client: OpenAI) -> None:
141message = client.beta.threads.messages.update(
142"string",
143thread_id="string",
144)
5429f696Stainless Bot2 years ago145assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago146
147@parametrize
148def test_method_update_with_all_params(self, client: OpenAI) -> None:
149message = client.beta.threads.messages.update(
150"string",
151thread_id="string",
152metadata={},
153)
5429f696Stainless Bot2 years ago154assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago155
156@parametrize
157def test_raw_response_update(self, client: OpenAI) -> None:
158response = client.beta.threads.messages.with_raw_response.update(
159"string",
160thread_id="string",
161)
86379b44Stainless Bot2 years ago162
163assert response.is_closed is True
baa9f07fRobert Craigie2 years ago164assert response.http_request.headers.get("X-Stainless-Lang") == "python"
165message = response.parse()
5429f696Stainless Bot2 years ago166assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago167
86379b44Stainless Bot2 years ago168@parametrize
169def test_streaming_response_update(self, client: OpenAI) -> None:
170with client.beta.threads.messages.with_streaming_response.update(
171"string",
172thread_id="string",
173) as response:
174assert not response.is_closed
175assert response.http_request.headers.get("X-Stainless-Lang") == "python"
176
177message = response.parse()
5429f696Stainless Bot2 years ago178assert_matches_type(Message, message, path=["response"])
86379b44Stainless Bot2 years ago179
180assert cast(Any, response.is_closed) is True
181
023a4e66Stainless Bot2 years ago182@parametrize
183def test_path_params_update(self, client: OpenAI) -> None:
184with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
185client.beta.threads.messages.with_raw_response.update(
186"string",
187thread_id="",
188)
189
190with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_id` but received ''"):
191client.beta.threads.messages.with_raw_response.update(
192"",
193thread_id="string",
194)
195
baa9f07fRobert Craigie2 years ago196@parametrize
197def test_method_list(self, client: OpenAI) -> None:
198message = client.beta.threads.messages.list(
199"string",
200)
5429f696Stainless Bot2 years ago201assert_matches_type(SyncCursorPage[Message], message, path=["response"])
baa9f07fRobert Craigie2 years ago202
203@parametrize
204def test_method_list_with_all_params(self, client: OpenAI) -> None:
205message = client.beta.threads.messages.list(
206"string",
207after="string",
208before="string",
209limit=0,
210order="asc",
8efca3a3Stainless Bot2 years ago211run_id="string",
baa9f07fRobert Craigie2 years ago212)
5429f696Stainless Bot2 years ago213assert_matches_type(SyncCursorPage[Message], message, path=["response"])
baa9f07fRobert Craigie2 years ago214
215@parametrize
216def test_raw_response_list(self, client: OpenAI) -> None:
217response = client.beta.threads.messages.with_raw_response.list(
218"string",
219)
86379b44Stainless Bot2 years ago220
221assert response.is_closed is True
baa9f07fRobert Craigie2 years ago222assert response.http_request.headers.get("X-Stainless-Lang") == "python"
223message = response.parse()
5429f696Stainless Bot2 years ago224assert_matches_type(SyncCursorPage[Message], message, path=["response"])
baa9f07fRobert Craigie2 years ago225
86379b44Stainless Bot2 years ago226@parametrize
227def test_streaming_response_list(self, client: OpenAI) -> None:
228with client.beta.threads.messages.with_streaming_response.list(
229"string",
230) as response:
231assert not response.is_closed
232assert response.http_request.headers.get("X-Stainless-Lang") == "python"
233
234message = response.parse()
5429f696Stainless Bot2 years ago235assert_matches_type(SyncCursorPage[Message], message, path=["response"])
86379b44Stainless Bot2 years ago236
237assert cast(Any, response.is_closed) is True
238
023a4e66Stainless Bot2 years ago239@parametrize
240def test_path_params_list(self, client: OpenAI) -> None:
241with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
242client.beta.threads.messages.with_raw_response.list(
243"",
244)
245
baa9f07fRobert Craigie2 years ago246
247class TestAsyncMessages:
98d779fbStainless Bot2 years ago248parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"])
baa9f07fRobert Craigie2 years ago249
250@parametrize
98d779fbStainless Bot2 years ago251async def test_method_create(self, async_client: AsyncOpenAI) -> None:
252message = await async_client.beta.threads.messages.create(
baa9f07fRobert Craigie2 years ago253"string",
254content="x",
255role="user",
256)
5429f696Stainless Bot2 years ago257assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago258
259@parametrize
98d779fbStainless Bot2 years ago260async def test_method_create_with_all_params(self, async_client: AsyncOpenAI) -> None:
261message = await async_client.beta.threads.messages.create(
baa9f07fRobert Craigie2 years ago262"string",
263content="x",
264role="user",
5b20698dStainless Bot2 years ago265attachments=[
266{
267"file_id": "string",
f73996b7Stainless Bot2 years ago268"tools": [{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}],
5b20698dStainless Bot2 years ago269},
270{
271"file_id": "string",
f73996b7Stainless Bot2 years ago272"tools": [{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}],
5b20698dStainless Bot2 years ago273},
274{
275"file_id": "string",
f73996b7Stainless Bot2 years ago276"tools": [{"type": "code_interpreter"}, {"type": "code_interpreter"}, {"type": "code_interpreter"}],
5b20698dStainless Bot2 years ago277},
278],
baa9f07fRobert Craigie2 years ago279metadata={},
280)
5429f696Stainless Bot2 years ago281assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago282
283@parametrize
98d779fbStainless Bot2 years ago284async def test_raw_response_create(self, async_client: AsyncOpenAI) -> None:
285response = await async_client.beta.threads.messages.with_raw_response.create(
baa9f07fRobert Craigie2 years ago286"string",
287content="x",
288role="user",
289)
86379b44Stainless Bot2 years ago290
291assert response.is_closed is True
baa9f07fRobert Craigie2 years ago292assert response.http_request.headers.get("X-Stainless-Lang") == "python"
293message = response.parse()
5429f696Stainless Bot2 years ago294assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago295
86379b44Stainless Bot2 years ago296@parametrize
98d779fbStainless Bot2 years ago297async def test_streaming_response_create(self, async_client: AsyncOpenAI) -> None:
298async with async_client.beta.threads.messages.with_streaming_response.create(
86379b44Stainless Bot2 years ago299"string",
300content="x",
301role="user",
302) as response:
303assert not response.is_closed
304assert response.http_request.headers.get("X-Stainless-Lang") == "python"
305
306message = await response.parse()
5429f696Stainless Bot2 years ago307assert_matches_type(Message, message, path=["response"])
86379b44Stainless Bot2 years ago308
309assert cast(Any, response.is_closed) is True
310
023a4e66Stainless Bot2 years ago311@parametrize
98d779fbStainless Bot2 years ago312async def test_path_params_create(self, async_client: AsyncOpenAI) -> None:
023a4e66Stainless Bot2 years ago313with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
98d779fbStainless Bot2 years ago314await async_client.beta.threads.messages.with_raw_response.create(
023a4e66Stainless Bot2 years ago315"",
316content="x",
317role="user",
318)
319
baa9f07fRobert Craigie2 years ago320@parametrize
98d779fbStainless Bot2 years ago321async def test_method_retrieve(self, async_client: AsyncOpenAI) -> None:
322message = await async_client.beta.threads.messages.retrieve(
baa9f07fRobert Craigie2 years ago323"string",
324thread_id="string",
325)
5429f696Stainless Bot2 years ago326assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago327
328@parametrize
98d779fbStainless Bot2 years ago329async def test_raw_response_retrieve(self, async_client: AsyncOpenAI) -> None:
330response = await async_client.beta.threads.messages.with_raw_response.retrieve(
baa9f07fRobert Craigie2 years ago331"string",
332thread_id="string",
333)
86379b44Stainless Bot2 years ago334
335assert response.is_closed is True
baa9f07fRobert Craigie2 years ago336assert response.http_request.headers.get("X-Stainless-Lang") == "python"
337message = response.parse()
5429f696Stainless Bot2 years ago338assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago339
86379b44Stainless Bot2 years ago340@parametrize
98d779fbStainless Bot2 years ago341async def test_streaming_response_retrieve(self, async_client: AsyncOpenAI) -> None:
342async with async_client.beta.threads.messages.with_streaming_response.retrieve(
86379b44Stainless Bot2 years ago343"string",
344thread_id="string",
345) as response:
346assert not response.is_closed
347assert response.http_request.headers.get("X-Stainless-Lang") == "python"
348
349message = await response.parse()
5429f696Stainless Bot2 years ago350assert_matches_type(Message, message, path=["response"])
86379b44Stainless Bot2 years ago351
352assert cast(Any, response.is_closed) is True
353
023a4e66Stainless Bot2 years ago354@parametrize
98d779fbStainless Bot2 years ago355async def test_path_params_retrieve(self, async_client: AsyncOpenAI) -> None:
023a4e66Stainless Bot2 years ago356with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
98d779fbStainless Bot2 years ago357await async_client.beta.threads.messages.with_raw_response.retrieve(
023a4e66Stainless Bot2 years ago358"string",
359thread_id="",
360)
361
362with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_id` but received ''"):
98d779fbStainless Bot2 years ago363await async_client.beta.threads.messages.with_raw_response.retrieve(
023a4e66Stainless Bot2 years ago364"",
365thread_id="string",
366)
367
baa9f07fRobert Craigie2 years ago368@parametrize
98d779fbStainless Bot2 years ago369async def test_method_update(self, async_client: AsyncOpenAI) -> None:
370message = await async_client.beta.threads.messages.update(
baa9f07fRobert Craigie2 years ago371"string",
372thread_id="string",
373)
5429f696Stainless Bot2 years ago374assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago375
376@parametrize
98d779fbStainless Bot2 years ago377async def test_method_update_with_all_params(self, async_client: AsyncOpenAI) -> None:
378message = await async_client.beta.threads.messages.update(
baa9f07fRobert Craigie2 years ago379"string",
380thread_id="string",
381metadata={},
382)
5429f696Stainless Bot2 years ago383assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago384
385@parametrize
98d779fbStainless Bot2 years ago386async def test_raw_response_update(self, async_client: AsyncOpenAI) -> None:
387response = await async_client.beta.threads.messages.with_raw_response.update(
baa9f07fRobert Craigie2 years ago388"string",
389thread_id="string",
390)
86379b44Stainless Bot2 years ago391
392assert response.is_closed is True
baa9f07fRobert Craigie2 years ago393assert response.http_request.headers.get("X-Stainless-Lang") == "python"
394message = response.parse()
5429f696Stainless Bot2 years ago395assert_matches_type(Message, message, path=["response"])
baa9f07fRobert Craigie2 years ago396
86379b44Stainless Bot2 years ago397@parametrize
98d779fbStainless Bot2 years ago398async def test_streaming_response_update(self, async_client: AsyncOpenAI) -> None:
399async with async_client.beta.threads.messages.with_streaming_response.update(
86379b44Stainless Bot2 years ago400"string",
401thread_id="string",
402) as response:
403assert not response.is_closed
404assert response.http_request.headers.get("X-Stainless-Lang") == "python"
405
406message = await response.parse()
5429f696Stainless Bot2 years ago407assert_matches_type(Message, message, path=["response"])
86379b44Stainless Bot2 years ago408
409assert cast(Any, response.is_closed) is True
410
023a4e66Stainless Bot2 years ago411@parametrize
98d779fbStainless Bot2 years ago412async def test_path_params_update(self, async_client: AsyncOpenAI) -> None:
023a4e66Stainless Bot2 years ago413with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
98d779fbStainless Bot2 years ago414await async_client.beta.threads.messages.with_raw_response.update(
023a4e66Stainless Bot2 years ago415"string",
416thread_id="",
417)
418
419with pytest.raises(ValueError, match=r"Expected a non-empty value for `message_id` but received ''"):
98d779fbStainless Bot2 years ago420await async_client.beta.threads.messages.with_raw_response.update(
023a4e66Stainless Bot2 years ago421"",
422thread_id="string",
423)
424
baa9f07fRobert Craigie2 years ago425@parametrize
98d779fbStainless Bot2 years ago426async def test_method_list(self, async_client: AsyncOpenAI) -> None:
427message = await async_client.beta.threads.messages.list(
baa9f07fRobert Craigie2 years ago428"string",
429)
5429f696Stainless Bot2 years ago430assert_matches_type(AsyncCursorPage[Message], message, path=["response"])
baa9f07fRobert Craigie2 years ago431
432@parametrize
98d779fbStainless Bot2 years ago433async def test_method_list_with_all_params(self, async_client: AsyncOpenAI) -> None:
434message = await async_client.beta.threads.messages.list(
baa9f07fRobert Craigie2 years ago435"string",
436after="string",
437before="string",
438limit=0,
439order="asc",
8efca3a3Stainless Bot2 years ago440run_id="string",
baa9f07fRobert Craigie2 years ago441)
5429f696Stainless Bot2 years ago442assert_matches_type(AsyncCursorPage[Message], message, path=["response"])
baa9f07fRobert Craigie2 years ago443
444@parametrize
98d779fbStainless Bot2 years ago445async def test_raw_response_list(self, async_client: AsyncOpenAI) -> None:
446response = await async_client.beta.threads.messages.with_raw_response.list(
baa9f07fRobert Craigie2 years ago447"string",
448)
86379b44Stainless Bot2 years ago449
450assert response.is_closed is True
baa9f07fRobert Craigie2 years ago451assert response.http_request.headers.get("X-Stainless-Lang") == "python"
452message = response.parse()
5429f696Stainless Bot2 years ago453assert_matches_type(AsyncCursorPage[Message], message, path=["response"])
86379b44Stainless Bot2 years ago454
455@parametrize
98d779fbStainless Bot2 years ago456async def test_streaming_response_list(self, async_client: AsyncOpenAI) -> None:
457async with async_client.beta.threads.messages.with_streaming_response.list(
86379b44Stainless Bot2 years ago458"string",
459) as response:
460assert not response.is_closed
461assert response.http_request.headers.get("X-Stainless-Lang") == "python"
462
463message = await response.parse()
5429f696Stainless Bot2 years ago464assert_matches_type(AsyncCursorPage[Message], message, path=["response"])
86379b44Stainless Bot2 years ago465
466assert cast(Any, response.is_closed) is True
023a4e66Stainless Bot2 years ago467
468@parametrize
98d779fbStainless Bot2 years ago469async def test_path_params_list(self, async_client: AsyncOpenAI) -> None:
023a4e66Stainless Bot2 years ago470with pytest.raises(ValueError, match=r"Expected a non-empty value for `thread_id` but received ''"):
98d779fbStainless Bot2 years ago471await async_client.beta.threads.messages.with_raw_response.list(
023a4e66Stainless Bot2 years ago472"",
473)