-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathtest_openai_model.py
More file actions
947 lines (779 loc) · 38.7 KB
/
Copy pathtest_openai_model.py
File metadata and controls
947 lines (779 loc) · 38.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
# Tencent is pleased to support the open source community by making tRPC-Agent-Python available.
#
# Copyright (C) 2026 Tencent. All rights reserved.
#
# tRPC-Agent-Python is licensed under Apache-2.0.
from unittest.mock import AsyncMock
from unittest.mock import Mock
from unittest.mock import patch
import pytest
from trpc_agent_sdk.models import LlmRequest
from trpc_agent_sdk.models import OpenAIModel
from trpc_agent_sdk.types import Content
from trpc_agent_sdk.types import GenerateContentConfig
from trpc_agent_sdk.types import Part
class TestOpenAIModel:
"""Test suite for OpenAIModel class."""
def test_init_basic(self):
"""Test basic initialization of OpenAIModel."""
model = OpenAIModel(model_name="gpt-4",
api_key="test_key",
base_url="https://custom.api.com",
add_tools_to_prompt=True,
tool_prompt="xml",
client_args={"timeout": 30})
assert model.name == "gpt-4"
assert model._api_key == "test_key"
assert model._base_url == "https://custom.api.com"
assert model.add_tools_to_prompt is True
assert model.tool_prompt == "xml"
assert model.client_args == {"timeout": 30}
def test_init_with_invalid_tool_prompt_string(self):
"""Test initialization with invalid tool prompt string raises error."""
with pytest.raises(ValueError, match="Invalid tool_prompt string"):
OpenAIModel(model_name="gpt-4", api_key="test_key", tool_prompt="invalid_format")
def test_init_without_optional_params(self):
"""Test initialization without optional parameters."""
model = OpenAIModel(model_name="gpt-4")
assert model._api_key == ""
assert model._base_url == ""
assert model.add_tools_to_prompt is False
assert model.tool_prompt == "xml"
def test_set_api_key_and_base_url(self):
"""Test setting API key and base URL."""
model = OpenAIModel(model_name="gpt-4", api_key="old_key", base_url="https://old.url.com")
model.set_api_key("new_key")
model.set_base_url("https://new.url.com")
assert model._api_key == "new_key"
assert model._base_url == "https://new.url.com"
@pytest.mark.parametrize("expected_model", ["gpt", "o1", "deepseek"])
def test_supported_models_patterns(self, expected_model):
"""Test that supported_models includes expected model patterns."""
supported = OpenAIModel.supported_models()
assert any(expected_model in pattern for pattern in supported)
def test_validate_request_with_valid_request(self):
"""Test validating a valid request passes."""
model = OpenAIModel(model_name="gpt-4", api_key="test_key")
content = Content(parts=[Part.from_text(text="Hello")], role="user")
request = LlmRequest(contents=[content], config=None, tools_dict={})
# Should not raise any exception
model.validate_request(request)
def test_validate_request_with_empty_contents(self):
"""Test validating request with empty contents raises ValueError."""
model = OpenAIModel(model_name="gpt-4", api_key="test_key")
request = LlmRequest(contents=[], config=None, tools_dict={})
with pytest.raises(ValueError, match="At least one content is required"):
model.validate_request(request)
def test_validate_request_with_empty_parts(self):
"""Test validating request with empty parts raises ValueError."""
model = OpenAIModel(model_name="gpt-4", api_key="test_key")
content = Content(parts=[], role="user")
request = LlmRequest(contents=[content], config=None, tools_dict={})
with pytest.raises(ValueError, match="Content must have at least one part"):
model.validate_request(request)
def test_validate_request_with_invalid_role(self):
"""Test validating request with invalid role raises ValueError."""
model = OpenAIModel(model_name="gpt-4", api_key="test_key")
content = Content(parts=[Part.from_text(text="Hello")], role="invalid_role")
request = LlmRequest(contents=[content], config=None, tools_dict={})
with pytest.raises(ValueError, match="Invalid content role"):
model.validate_request(request)
@pytest.mark.parametrize("role", ["user", "assistant", "model", "system"])
def test_validate_request_with_valid_roles(self, role):
"""Test validating request with all valid roles."""
model = OpenAIModel(model_name="gpt-4", api_key="test_key")
content = Content(parts=[Part.from_text(text="Hello")], role=role)
request = LlmRequest(contents=[content], config=None, tools_dict={})
# Should not raise
model.validate_request(request)
def test_validate_request_with_function_call(self):
"""Test validating request with function call in parts."""
model = OpenAIModel(model_name="gpt-4", api_key="test_key")
part = Part.from_function_call(name="test_function", args={"param": "value"})
content = Content(parts=[part], role="assistant")
request = LlmRequest(contents=[content], config=None, tools_dict={})
# Should not raise
model.validate_request(request)
def test_validate_request_with_function_response(self):
"""Test validating request with function response in parts."""
model = OpenAIModel(model_name="gpt-4", api_key="test_key")
part = Part.from_function_response(name="test_function", response={"result": "success"})
content = Content(parts=[part], role="user")
request = LlmRequest(contents=[content], config=None, tools_dict={})
# Should not raise
model.validate_request(request)
def test_validate_request_with_multiple_contents(self):
"""Test validating request with multiple contents."""
model = OpenAIModel(model_name="gpt-4", api_key="test_key")
content1 = Content(parts=[Part.from_text(text="First message")], role="user")
content2 = Content(parts=[Part.from_text(text="Second message")], role="assistant")
content3 = Content(parts=[Part.from_text(text="Third message")], role="user")
request = LlmRequest(contents=[content1, content2, content3], config=None, tools_dict={})
# Should not raise
model.validate_request(request)
def test_properties_and_config(self):
"""Test model properties and config."""
model = OpenAIModel(
model_name="gpt-4-turbo",
api_key="test_key",
custom_param1="value1",
custom_param2="value2",
)
assert model.name == "gpt-4-turbo"
assert model.display_name == "OpenAIModel"
assert model.config.get("custom_param1") == "value1"
assert model.config.get("custom_param2") == "value2"
def test_validate_request_with_multiple_parts_in_content(self):
"""Test validating request with multiple parts in a single content."""
model = OpenAIModel(model_name="gpt-4", api_key="test_key")
parts = [Part.from_text(text="Part 1"), Part.from_text(text="Part 2"), Part.from_text(text="Part 3")]
content = Content(parts=parts, role="user")
request = LlmRequest(contents=[content], config=None, tools_dict={})
# Should not raise
model.validate_request(request)
def test_validate_request_with_inline_data(self):
"""Test validating request with inline_data (image) in parts."""
model = OpenAIModel(model_name="gpt-4", api_key="test_key")
# Create a part with inline image data
part = Part()
part.inline_data = Mock()
part.inline_data.mime_type = "image/png"
part.inline_data.data = b"fake_image_data"
content = Content(parts=[part], role="user")
request = LlmRequest(contents=[content], config=None, tools_dict={})
# Should not raise
model.validate_request(request)
def test_validate_request_with_code_execution_result(self):
"""Test validating request with code_execution_result in parts."""
model = OpenAIModel(model_name="gpt-4", api_key="test_key")
part = Part()
part.code_execution_result = Mock()
part.code_execution_result.outcome = Mock()
part.code_execution_result.outcome.value = "success"
part.code_execution_result.output = "Result output"
content = Content(parts=[part], role="user")
request = LlmRequest(contents=[content], config=None, tools_dict={})
# Should not raise
model.validate_request(request)
def test_validate_request_with_executable_code(self):
"""Test validating request with executable_code in parts."""
model = OpenAIModel(model_name="gpt-4", api_key="test_key")
part = Part()
part.executable_code = Mock()
part.executable_code.language = Mock()
part.executable_code.language.value = "python"
part.executable_code.code = "print('hello')"
content = Content(parts=[part], role="user")
request = LlmRequest(contents=[content], config=None, tools_dict={})
# Should not raise
model.validate_request(request)
def test_validate_request_with_config(self):
"""Test validating request with GenerateContentConfig."""
model = OpenAIModel(model_name="gpt-4", api_key="test_key")
config = GenerateContentConfig(max_output_tokens=100, temperature=0.7)
content = Content(parts=[Part.from_text(text="Hello")], role="user")
request = LlmRequest(contents=[content], config=config, tools_dict={})
# Should not raise
model.validate_request(request)
def test_model_type_is_model(self):
"""Test that the model's filter type is MODEL."""
from trpc_agent_sdk.filter import FilterType
model = OpenAIModel(model_name="gpt-4", api_key="test_key")
assert model._type == FilterType.MODEL
def test_create_async_client_uses_custom_http_client_provider(self):
"""A custom http_client_provider_factory is passed through to AsyncOpenAI."""
shared_http_client = Mock()
http_client_provider = Mock()
http_client_provider.create_http_client.return_value = shared_http_client
http_client_provider_factory = Mock(return_value=http_client_provider)
model = OpenAIModel(
model_name="gpt-4",
api_key="test_key",
base_url="https://custom.api.com",
client_args={"timeout": 30},
http_client_provider_factory=http_client_provider_factory,
)
with patch("trpc_agent_sdk.models._openai_model.openai.AsyncOpenAI") as mock_async_openai:
client = model._create_async_client()
assert client is mock_async_openai.return_value
http_client_provider_factory.assert_called_once_with()
http_client_provider.create_http_client.assert_called_once_with()
mock_async_openai.assert_called_once_with(
api_key="test_key",
max_retries=0,
organization="",
base_url="https://custom.api.com",
timeout=30,
http_client=shared_http_client,
)
def test_create_async_client_default_factory_reuses_loop_local_http_client(self):
"""Default provider should reuse the same httpx.AsyncClient within one loop."""
from trpc_agent_sdk.models import _httpx_client
from trpc_agent_sdk.models import shared_http_client_provider_factory
shared_http_client = Mock()
shared_http_client.is_closed = False
model = OpenAIModel(model_name="gpt-4", api_key="test_key", http_client_provider_factory=shared_http_client_provider_factory)
try:
_httpx_client._shared_http_clients.clear()
with patch("trpc_agent_sdk.models._httpx_client.httpx.AsyncClient",
return_value=shared_http_client) as mock_httpx_client:
with patch("trpc_agent_sdk.models._httpx_client._get_loop_key", return_value=1):
with patch("trpc_agent_sdk.models._openai_model.openai.AsyncOpenAI") as mock_async_openai:
model._create_async_client()
model._create_async_client()
finally:
_httpx_client._shared_http_clients.clear()
mock_httpx_client.assert_called_once_with(
limits=_httpx_client._DEFAULT_HTTP_CLIENT_LIMITS,
timeout=_httpx_client._DEFAULT_HTTP_CLIENT_TIMEOUT,
follow_redirects=True,
)
first_call_kwargs = mock_async_openai.call_args_list[0].kwargs
second_call_kwargs = mock_async_openai.call_args_list[1].kwargs
assert first_call_kwargs["http_client"] is shared_http_client
assert second_call_kwargs["http_client"] is shared_http_client
def test_create_shared_http_client_rebuilds_closed_client(self):
"""Closed cached clients should be replaced on the next factory call."""
from trpc_agent_sdk.models import _httpx_client
closed_client = Mock()
closed_client.is_closed = True
fresh_client = Mock()
fresh_client.is_closed = False
try:
_httpx_client._shared_http_clients.clear()
client_key = (1234, 1)
_httpx_client._shared_http_clients[client_key] = closed_client
with patch("trpc_agent_sdk.models._httpx_client._get_client_key", return_value=client_key):
with patch("trpc_agent_sdk.models._httpx_client.httpx.AsyncClient",
return_value=fresh_client) as mock_httpx_client:
assert _httpx_client._create_shared_http_client() is fresh_client
finally:
_httpx_client._shared_http_clients.clear()
mock_httpx_client.assert_called_once()
def test_create_shared_http_client_does_not_reuse_across_loop_keys(self):
"""Different event loops should get different default httpx clients."""
from trpc_agent_sdk.models import _httpx_client
first_client = Mock()
first_client.is_closed = False
second_client = Mock()
second_client.is_closed = False
try:
_httpx_client._shared_http_clients.clear()
with patch("trpc_agent_sdk.models._httpx_client.httpx.AsyncClient",
side_effect=[first_client, second_client]) as mock_httpx_client:
with patch("trpc_agent_sdk.models._httpx_client._get_client_key", side_effect=[(1234, 1), (1234, 2)]):
assert _httpx_client._create_shared_http_client() is first_client
assert _httpx_client._create_shared_http_client() is second_client
finally:
_httpx_client._shared_http_clients.clear()
assert mock_httpx_client.call_count == 2
def test_create_shared_http_client_does_not_reuse_across_process_keys(self):
"""Different process keys should get different default httpx clients."""
from trpc_agent_sdk.models import _httpx_client
parent_client = Mock()
parent_client.is_closed = False
child_client = Mock()
child_client.is_closed = False
try:
_httpx_client._shared_http_clients.clear()
with patch("trpc_agent_sdk.models._httpx_client.httpx.AsyncClient",
side_effect=[parent_client, child_client]) as mock_httpx_client:
with patch("trpc_agent_sdk.models._httpx_client._get_client_key",
side_effect=[(1234, 1), (5678, 1)]):
assert _httpx_client._create_shared_http_client() is parent_client
assert _httpx_client._create_shared_http_client() is child_client
finally:
_httpx_client._shared_http_clients.clear()
assert mock_httpx_client.call_count == 2
def test_reset_shared_http_clients_after_fork_clears_cache_and_rebuilds_lock(self):
"""Fork child reset should drop inherited clients and replace inherited locks."""
from trpc_agent_sdk.models import _httpx_client
inherited_client = Mock()
old_lock = _httpx_client._shared_http_clients_lock
try:
_httpx_client._shared_http_clients[(1234, 1)] = inherited_client
_httpx_client._reset_shared_http_clients_after_fork()
assert _httpx_client._shared_http_clients == {}
assert _httpx_client._shared_http_clients_lock is not old_lock
finally:
_httpx_client._shared_http_clients.clear()
def test_create_async_client_overwrites_stale_client_args_http_client(self):
"""Provider owns http_client injection even if client_args already has one."""
stale_http_client = Mock()
fresh_http_client = Mock()
http_client_provider = Mock()
http_client_provider.create_http_client.return_value = fresh_http_client
http_client_provider_factory = Mock(return_value=http_client_provider)
model = OpenAIModel(
model_name="gpt-4",
api_key="test_key",
client_args={"http_client": stale_http_client, "timeout": 30},
http_client_provider_factory=http_client_provider_factory,
)
with patch("trpc_agent_sdk.models._openai_model.openai.AsyncOpenAI") as mock_async_openai:
model._create_async_client()
assert mock_async_openai.call_args.kwargs["http_client"] is fresh_http_client
assert mock_async_openai.call_args.kwargs["timeout"] == 30
# ==================== Tests for generate_async method ====================
@pytest.mark.asyncio
async def test_generate_async_simple_text_response(self):
"""Test generate_async with a simple text response."""
model = OpenAIModel(model_name="gpt-4", api_key="test_key")
content = Content(parts=[Part.from_text(text="Hello")], role="user")
config = GenerateContentConfig(max_output_tokens=100)
request = LlmRequest(contents=[content], config=config, tools_dict={})
# Mock the async client
mock_response = Mock()
mock_response.model_dump.return_value = {
"choices": [{
"message": {
"content": "Hello, how can I help you?",
"role": "assistant"
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 20,
"total_tokens": 30
}
}
with patch.object(model, '_create_async_client') as mock_client_factory:
mock_client = AsyncMock()
mock_client.chat.completions.create = AsyncMock(return_value=mock_response)
mock_client.close = AsyncMock()
mock_client_factory.return_value = mock_client
responses = []
async for response in model.generate_async(request, stream=False):
responses.append(response)
assert len(responses) == 1
assert responses[0].content is not None
assert responses[0].content.parts[0].text == "Hello, how can I help you?"
assert responses[0].usage_metadata.prompt_token_count == 10
@pytest.mark.asyncio
async def test_generate_async_validation_failure(self):
"""Test generate_async returns an error response on invalid request."""
model = OpenAIModel(model_name="gpt-4", api_key="test_key")
# Empty contents
request = LlmRequest(contents=[], config=None, tools_dict={})
responses = []
async for response in model.generate_async(request, stream=False):
responses.append(response)
assert len(responses) == 1
assert responses[0].error_code == "API_ERROR"
assert "At least one content is required" in responses[0].error_message
@pytest.mark.asyncio
async def test_generate_async_with_config_parameters(self):
"""Test generate_async respects configuration parameters."""
model = OpenAIModel(model_name="gpt-4", api_key="test_key")
content = Content(parts=[Part.from_text(text="Generate a story")], role="user")
config = GenerateContentConfig(max_output_tokens=500, temperature=0.8, top_p=0.9, stop_sequences=["END"])
request = LlmRequest(contents=[content], config=config, tools_dict={})
mock_response = Mock()
mock_response.model_dump.return_value = {
"choices": [{
"message": {
"content": "Once upon a time...",
"role": "assistant"
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 5,
"completion_tokens": 100,
"total_tokens": 105
}
}
with patch.object(model, '_create_async_client') as mock_client_factory:
mock_client = AsyncMock()
mock_client.chat.completions.create = AsyncMock(return_value=mock_response)
mock_client.close = AsyncMock()
mock_client_factory.return_value = mock_client
captured_api_params = None
async def capture_create(**kwargs):
nonlocal captured_api_params
captured_api_params = kwargs
return mock_response
mock_client.chat.completions.create = capture_create
responses = []
async for response in model.generate_async(request, stream=False):
responses.append(response)
assert len(responses) == 1
@pytest.mark.asyncio
async def test_generate_async_with_function_call_response(self):
"""Test generate_async with tool/function call response."""
model = OpenAIModel(model_name="gpt-4", api_key="test_key")
content = Content(parts=[Part.from_text(text="Call a function")], role="user")
request = LlmRequest(contents=[content], config=None, tools_dict={})
mock_response = Mock()
mock_response.model_dump.return_value = {
"choices": [{
"message": {
"content":
None,
"role":
"assistant",
"tool_calls": [{
"id": "call_123",
"type": "function",
"function": {
"name": "get_weather",
"arguments": '{"location": "New York"}'
}
}]
},
"finish_reason": "tool_calls"
}],
"usage": {
"prompt_tokens": 10,
"completion_tokens": 50,
"total_tokens": 60
}
}
with patch.object(model, '_create_async_client') as mock_client_factory:
mock_client = AsyncMock()
mock_client.chat.completions.create = AsyncMock(return_value=mock_response)
mock_client.close = AsyncMock()
mock_client_factory.return_value = mock_client
responses = []
async for response in model.generate_async(request, stream=False):
responses.append(response)
assert len(responses) == 1
assert responses[0].content is not None
# Should have function call part
function_parts = [p for p in responses[0].content.parts if p.function_call]
assert len(function_parts) > 0
@pytest.mark.asyncio
async def test_generate_async_streaming_mode(self):
"""Test generate_async in streaming mode."""
model = OpenAIModel(model_name="gpt-4", api_key="test_key")
content = Content(parts=[Part.from_text(text="Tell me a story")], role="user")
request = LlmRequest(contents=[content], config=None, tools_dict={})
# Create mock streaming response
chunk1 = Mock()
chunk1.model_dump.return_value = {
"choices": [{
"delta": {
"content": "Once "
},
"finish_reason": None,
"index": 0
}],
"usage": None
}
chunk2 = Mock()
chunk2.model_dump.return_value = {
"choices": [{
"delta": {
"content": "upon "
},
"finish_reason": None,
"index": 0
}],
"usage": None
}
chunk3 = Mock()
chunk3.model_dump.return_value = {
"choices": [{
"delta": {
"content": "a time..."
},
"finish_reason": "stop",
"index": 0
}],
"usage": {
"prompt_tokens": 5,
"completion_tokens": 20,
"total_tokens": 25
}
}
async def mock_stream():
yield chunk1
yield chunk2
yield chunk3
mock_response = mock_stream()
with patch.object(model, '_create_async_client') as mock_client_factory:
mock_client = AsyncMock()
mock_client.chat.completions.create = AsyncMock(return_value=mock_response)
mock_client.close = AsyncMock()
mock_client_factory.return_value = mock_client
responses = []
async for response in model.generate_async(request, stream=True):
responses.append(response)
# Should have multiple partial responses plus final response
assert len(responses) > 1
@pytest.mark.asyncio
async def test_generate_async_error_handling(self):
"""Test generate_async handles API errors gracefully."""
model = OpenAIModel(model_name="gpt-4", api_key="test_key")
content = Content(parts=[Part.from_text(text="Hello")], role="user")
request = LlmRequest(contents=[content], config=None, tools_dict={})
with patch.object(model, '_create_async_client') as mock_client_factory:
mock_client = AsyncMock()
mock_client.chat.completions.create = AsyncMock(side_effect=Exception("API Error: Invalid API key"))
mock_client.close = AsyncMock()
mock_client_factory.return_value = mock_client
responses = []
async for response in model.generate_async(request, stream=False):
responses.append(response)
assert len(responses) == 1
assert responses[0].error_code == "API_ERROR"
assert "Invalid API key" in responses[0].error_message
@pytest.mark.asyncio
async def test_generate_async_with_system_instruction(self):
"""Test generate_async with system instruction in config."""
model = OpenAIModel(model_name="gpt-4", api_key="test_key")
content = Content(parts=[Part.from_text(text="Hello")], role="user")
config = GenerateContentConfig(system_instruction="You are a helpful assistant.")
request = LlmRequest(contents=[content], config=config, tools_dict={})
mock_response = Mock()
mock_response.model_dump.return_value = {
"choices": [{
"message": {
"content": "Hello! How can I assist you?",
"role": "assistant"
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 15,
"completion_tokens": 10,
"total_tokens": 25
}
}
with patch.object(model, '_create_async_client') as mock_client_factory:
mock_client = AsyncMock()
mock_client.chat.completions.create = AsyncMock(return_value=mock_response)
mock_client.close = AsyncMock()
mock_client_factory.return_value = mock_client
responses = []
async for response in model.generate_async(request, stream=False):
responses.append(response)
assert len(responses) == 1
assert responses[0].content.parts[0].text == "Hello! How can I assist you?"
@pytest.mark.asyncio
async def test_generate_async_with_multiple_contents(self):
"""Test generate_async with conversation history (multiple contents)."""
model = OpenAIModel(model_name="gpt-4", api_key="test_key")
content1 = Content(parts=[Part.from_text(text="Hi")], role="user")
content2 = Content(parts=[Part.from_text(text="Hello! How can I help?")], role="assistant")
content3 = Content(parts=[Part.from_text(text="What's the weather?")], role="user")
config = GenerateContentConfig(max_output_tokens=100)
request = LlmRequest(contents=[content1, content2, content3], config=config, tools_dict={})
mock_response = Mock()
mock_response.model_dump.return_value = {
"choices": [{
"message": {
"content": "I don't have access to real-time weather data.",
"role": "assistant"
},
"finish_reason": "stop"
}],
"usage": {
"prompt_tokens": 20,
"completion_tokens": 15,
"total_tokens": 35
}
}
with patch.object(model, '_create_async_client') as mock_client_factory:
mock_client = AsyncMock()
mock_client.chat.completions.create = AsyncMock(return_value=mock_response)
mock_client.close = AsyncMock()
mock_client_factory.return_value = mock_client
responses = []
async for response in model.generate_async(request, stream=False):
responses.append(response)
assert len(responses) == 1
assert "weather" in responses[0].content.parts[0].text.lower()
@pytest.mark.asyncio
async def test_generate_async_empty_response_content(self):
"""Test generate_async handles empty response content."""
model = OpenAIModel(model_name="gpt-4", api_key="test_key")
content = Content(parts=[Part.from_text(text="Generate something")], role="user")
request = LlmRequest(contents=[content], config=None, tools_dict={})
mock_response = Mock()
mock_response.model_dump.return_value = {
"choices": [{
"message": {
"content": None,
"role": "assistant"
},
"finish_reason": "length"
}],
"usage": {
"prompt_tokens": 5,
"completion_tokens": 0,
"total_tokens": 5
}
}
with patch.object(model, '_create_async_client') as mock_client_factory:
mock_client = AsyncMock()
mock_client.chat.completions.create = AsyncMock(return_value=mock_response)
mock_client.close = AsyncMock()
mock_client_factory.return_value = mock_client
responses = []
async for response in model.generate_async(request, stream=False):
responses.append(response)
assert len(responses) == 1
assert responses[0].content is not None
# ===========================================================================
# Prompt cache — request field injection
# ===========================================================================
class TestOpenAIPromptCacheRequestFields:
"""Verify prompt cache fields are added to api_params when cache config is active."""
def _make_api_params(self) -> dict:
"""Minimal api_params skeleton similar to what OpenAIModel builds."""
return {"model": "gpt-4", "messages": [{"role": "user", "content": "hi"}]}
def _simulate_cache_injection(self, model, api_params: dict) -> None:
"""Replicate the inline cache injection logic from _generate_async_impl."""
from trpc_agent_sdk.models._openai_model import ApiParamsKey
cache_config = model._resolve_prompt_cache_config(None)
if cache_config:
if cache_config.prompt_cache_key:
api_params[ApiParamsKey.PROMPT_CACHE_KEY] = cache_config.prompt_cache_key
if cache_config.ttl:
api_params[ApiParamsKey.PROMPT_CACHE_RETENTION] = cache_config.ttl
def test_enabled_config_adds_cache_key_and_retention(self):
"""Both prompt_cache_key and prompt_cache_retention are forwarded when set."""
from trpc_agent_sdk.configs import PromptCacheConfig
model = OpenAIModel(
model_name="gpt-4",
api_key="k",
prompt_cache_config=PromptCacheConfig(
enabled=True,
prompt_cache_key="weather-v1",
ttl="24h",
),
)
api_params = self._make_api_params()
self._simulate_cache_injection(model, api_params)
assert api_params.get("prompt_cache_key") == "weather-v1"
assert api_params.get("prompt_cache_retention") == "24h"
def test_ttl_in_memory_is_forwarded(self):
"""'in_memory' is forwarded as prompt_cache_retention."""
from trpc_agent_sdk.configs import PromptCacheConfig
model = OpenAIModel(
model_name="gpt-4",
api_key="k",
prompt_cache_config=PromptCacheConfig(enabled=True, ttl="in_memory"),
)
api_params = self._make_api_params()
self._simulate_cache_injection(model, api_params)
assert api_params.get("prompt_cache_retention") == "in_memory"
def test_custom_ttl_is_forwarded(self):
"""TTL is provider-specific and should be forwarded without SDK validation."""
from trpc_agent_sdk.configs import PromptCacheConfig
model = OpenAIModel(
model_name="gpt-4",
api_key="k",
prompt_cache_config=PromptCacheConfig(enabled=True, ttl="1h"),
)
api_params = self._make_api_params()
self._simulate_cache_injection(model, api_params)
assert api_params.get("prompt_cache_retention") == "1h"
def test_disabled_config_adds_no_cache_fields(self):
"""Disabled PromptCacheConfig must not inject any cache-related keys."""
from trpc_agent_sdk.configs import PromptCacheConfig
from trpc_agent_sdk.models._openai_model import ApiParamsKey
model = OpenAIModel(
model_name="gpt-4",
api_key="k",
prompt_cache_config=PromptCacheConfig(enabled=False, prompt_cache_key="k", ttl="24h"),
)
api_params = self._make_api_params()
self._simulate_cache_injection(model, api_params)
assert ApiParamsKey.PROMPT_CACHE_KEY not in api_params
assert ApiParamsKey.PROMPT_CACHE_RETENTION not in api_params
def test_no_config_adds_no_cache_fields(self):
"""No model-level config means no cache keys are added."""
from trpc_agent_sdk.models._openai_model import ApiParamsKey
model = OpenAIModel(model_name="gpt-4", api_key="k")
api_params = self._make_api_params()
self._simulate_cache_injection(model, api_params)
assert ApiParamsKey.PROMPT_CACHE_KEY not in api_params
assert ApiParamsKey.PROMPT_CACHE_RETENTION not in api_params
def test_config_without_cache_key_omits_key_field(self):
"""prompt_cache_key not in api_params when config has no prompt_cache_key."""
from trpc_agent_sdk.configs import PromptCacheConfig
from trpc_agent_sdk.models._openai_model import ApiParamsKey
model = OpenAIModel(
model_name="gpt-4",
api_key="k",
prompt_cache_config=PromptCacheConfig(enabled=True, ttl="24h"),
)
api_params = self._make_api_params()
self._simulate_cache_injection(model, api_params)
assert ApiParamsKey.PROMPT_CACHE_KEY not in api_params
assert api_params.get("prompt_cache_retention") == "24h"
# ===========================================================================
# Prompt cache — usage metadata parsing
# ===========================================================================
class TestOpenAIBuildUsageMetadata:
"""Tests for OpenAIModel._build_usage_metadata cache token normalization."""
def test_prompt_tokens_details_cached_tokens_mapped_to_cache_read(self):
"""OpenAI prompt_tokens_details.cached_tokens maps to cache_read_input_tokens."""
usage_data = {
"prompt_tokens": 1000,
"completion_tokens": 50,
"total_tokens": 1050,
"prompt_tokens_details": {
"cached_tokens": 800
},
}
meta = OpenAIModel._build_usage_metadata(usage_data)
assert meta.cache_read_input_tokens == 800
assert meta.prompt_token_count == 1000
assert meta.candidates_token_count == 50
def test_top_level_cache_read_preferred_over_details(self):
"""If top-level cache_read_input_tokens is set, it wins over prompt_tokens_details."""
usage_data = {
"prompt_tokens": 1000,
"completion_tokens": 50,
"total_tokens": 1050,
"cache_read_input_tokens": 600,
"prompt_tokens_details": {
"cached_tokens": 800
},
}
meta = OpenAIModel._build_usage_metadata(usage_data)
assert meta.cache_read_input_tokens == 600
def test_no_cache_fields_yields_none(self):
"""When no cache fields are present, cache_read_input_tokens is None."""
usage_data = {
"prompt_tokens": 100,
"completion_tokens": 20,
"total_tokens": 120,
}
meta = OpenAIModel._build_usage_metadata(usage_data)
assert meta.cache_read_input_tokens is None
assert meta.cache_creation_input_tokens is None
def test_cache_creation_input_tokens_top_level(self):
"""top-level cache_creation_input_tokens (LiteLLM-compatible) is forwarded."""
usage_data = {
"prompt_tokens": 100,
"completion_tokens": 10,
"total_tokens": 110,
"cache_creation_input_tokens": 90,
}
meta = OpenAIModel._build_usage_metadata(usage_data)
assert meta.cache_creation_input_tokens == 90
def test_empty_prompt_tokens_details_does_not_crash(self):
"""Empty prompt_tokens_details dict is handled safely."""
usage_data = {
"prompt_tokens": 50,
"completion_tokens": 10,
"total_tokens": 60,
"prompt_tokens_details": {},
}
meta = OpenAIModel._build_usage_metadata(usage_data)
assert meta.cache_read_input_tokens is None
def test_null_prompt_tokens_details_does_not_crash(self):
"""Explicit null prompt_tokens_details is handled safely."""
usage_data = {
"prompt_tokens": 50,
"completion_tokens": 10,
"total_tokens": 60,
"prompt_tokens_details": None,
}
meta = OpenAIModel._build_usage_metadata(usage_data)
assert meta.cache_read_input_tokens is None