openai/openai-python
Publicmirrored fromhttps://github.com/openai/openai-pythonAvailable
tests/lib/responses/test_responses.py
63lines · modecode
| 1 | from __future__ import annotations |
| 2 | |
| 3 | from typing_extensions import TypeVar |
| 4 | |
| 5 | import pytest |
| 6 | from respx import MockRouter |
| 7 | from inline_snapshot import snapshot |
| 8 | |
| 9 | from openai import OpenAI, AsyncOpenAI |
| 10 | from openai._utils import assert_signatures_in_sync |
| 11 | |
| 12 | from ...conftest import base_url |
| 13 | from ..snapshots import make_snapshot_request |
| 14 | |
| 15 | _T = TypeVar("_T") |
| 16 | |
| 17 | # all the snapshots in this file are auto-generated from the live API |
| 18 | # |
| 19 | # you can update them with |
| 20 | # |
| 21 | # `OPENAI_LIVE=1 pytest --inline-snapshot=fix -p no:xdist -o addopts=""` |
| 22 | |
| 23 | |
| 24 | @pytest.mark.respx(base_url=base_url) |
| 25 | def test_output_text(client: OpenAI, respx_mock: MockRouter) -> None: |
| 26 | response = make_snapshot_request( |
| 27 | lambda c: c.responses.create( |
| 28 | model="gpt-4o-mini", |
| 29 | input="What's the weather like in SF?", |
| 30 | ), |
| 31 | content_snapshot=snapshot( |
| 32 | '{"id": "resp_689a0b2545288193953c892439b42e2800b2e36c65a1fd4b", "object": "response", "created_at": 1754925861, "status": "completed", "background": false, "error": null, "incomplete_details": null, "instructions": null, "max_output_tokens": null, "max_tool_calls": null, "model": "gpt-4o-mini-2024-07-18", "output": [{"id": "msg_689a0b2637b08193ac478e568f49e3f900b2e36c65a1fd4b", "type": "message", "status": "completed", "content": [{"type": "output_text", "annotations": [], "logprobs": [], "text": "I can\'t provide real-time updates, but you can easily check the current weather in San Francisco using a weather website or app. Typically, San Francisco has cool, foggy summers and mild winters, so it\'s good to be prepared for variable weather!"}], "role": "assistant"}], "parallel_tool_calls": true, "previous_response_id": null, "prompt_cache_key": null, "reasoning": {"effort": null, "summary": null}, "safety_identifier": null, "service_tier": "default", "store": true, "temperature": 1.0, "text": {"format": {"type": "text"}, "verbosity": "medium"}, "tool_choice": "auto", "tools": [], "top_logprobs": 0, "top_p": 1.0, "truncation": "disabled", "usage": {"input_tokens": 14, "input_tokens_details": {"cached_tokens": 0}, "output_tokens": 50, "output_tokens_details": {"reasoning_tokens": 0}, "total_tokens": 64}, "user": null, "metadata": {}}' |
| 33 | ), |
| 34 | path="/responses", |
| 35 | mock_client=client, |
| 36 | respx_mock=respx_mock, |
| 37 | ) |
| 38 | |
| 39 | assert response.output_text == snapshot( |
| 40 | "I can't provide real-time updates, but you can easily check the current weather in San Francisco using a weather website or app. Typically, San Francisco has cool, foggy summers and mild winters, so it's good to be prepared for variable weather!" |
| 41 | ) |
| 42 | |
| 43 | |
| 44 | @pytest.mark.parametrize("sync", [True, False], ids=["sync", "async"]) |
| 45 | def test_stream_method_definition_in_sync(sync: bool, client: OpenAI, async_client: AsyncOpenAI) -> None: |
| 46 | checking_client: OpenAI | AsyncOpenAI = client if sync else async_client |
| 47 | |
| 48 | assert_signatures_in_sync( |
| 49 | checking_client.responses.create, |
| 50 | checking_client.responses.stream, |
| 51 | exclude_params={"stream", "tools"}, |
| 52 | ) |
| 53 | |
| 54 | |
| 55 | @pytest.mark.parametrize("sync", [True, False], ids=["sync", "async"]) |
| 56 | def test_parse_method_definition_in_sync(sync: bool, client: OpenAI, async_client: AsyncOpenAI) -> None: |
| 57 | checking_client: OpenAI | AsyncOpenAI = client if sync else async_client |
| 58 | |
| 59 | assert_signatures_in_sync( |
| 60 | checking_client.responses.create, |
| 61 | checking_client.responses.parse, |
| 62 | exclude_params={"tools"}, |
| 63 | ) |
| 64 | |