Repository navigation
Expand file tree
/
Copy pathtest_cohere.py
More file actions
771 lines (620 loc) · 25.3 KB
/
Copy pathtest_cohere.py
File metadata and controls
771 lines (620 loc) · 25.3 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
"""Tests for the Cohere integration."""
import asyncio
import inspect
import os
import time
from pathlib import Path
import pytest
from braintrust import Attachment, logger
from braintrust.integrations.cohere import CohereIntegration, wrap_cohere
from braintrust.integrations.cohere.patchers import (
AsyncChatPatcher,
AsyncChatStreamPatcher,
AsyncEmbedPatcher,
AsyncRerankPatcher,
AsyncTranscriptionsCreatePatcher,
AsyncV2ChatPatcher,
AsyncV2ChatStreamPatcher,
AsyncV2EmbedPatcher,
AsyncV2RerankPatcher,
ChatPatcher,
ChatStreamPatcher,
EmbedPatcher,
RerankPatcher,
TranscriptionsCreatePatcher,
V2ChatPatcher,
V2ChatStreamPatcher,
V2EmbedPatcher,
V2RerankPatcher,
)
from braintrust.integrations.test_utils import assert_metrics_are_valid, verify_autoinstrument_script
from braintrust.span_types import SpanTypeAttribute
from braintrust.test_helpers import find_spans_by_type, init_test_logger
pytest.importorskip("cohere")
import cohere # noqa: E402
PROJECT_NAME = "test-cohere-sdk"
CHAT_MODEL = "command-a-03-2025"
EMBED_MODEL = "embed-english-v3.0"
RERANK_MODEL = "rerank-english-v3.0"
TRANSCRIBE_MODEL = "cohere-transcribe-03-2026"
TEST_AUDIO_FILE = Path(__file__).resolve().parents[2] / "fixtures" / "test_audio.wav"
COHERE_API_KEY = os.getenv("CO_API_KEY") or os.getenv("COHERE_API_KEY") or "co-test-dummy-api-key-for-vcr-tests"
# --- Fixtures ---------------------------------------------------------------
@pytest.fixture
def memory_logger():
init_test_logger(PROJECT_NAME)
with logger._internal_with_memory_background_logger() as bgl:
yield bgl
def _supports_client(name: str, *methods: str) -> bool:
client = getattr(cohere, name, None)
return client is not None and all(hasattr(client, method) for method in methods)
def _v1_client():
return cohere.Client(api_key=COHERE_API_KEY)
def _v1_async_client():
return cohere.AsyncClient(api_key=COHERE_API_KEY)
def _v2_client(*, require_methods: tuple[str, ...] = ("chat",)):
if not _supports_client("ClientV2", *require_methods):
pytest.skip(f"Cohere ClientV2 missing required methods: {', '.join(require_methods)}")
return cohere.ClientV2(api_key=COHERE_API_KEY)
def _v2_async_client(*, require_methods: tuple[str, ...] = ("chat",)):
if not _supports_client("AsyncClientV2", *require_methods):
pytest.skip(f"Cohere AsyncClientV2 missing required methods: {', '.join(require_methods)}")
return cohere.AsyncClientV2(api_key=COHERE_API_KEY)
# A restoration context that snapshots and restores the patched methods on the
# Cohere classes. Integration setup mutates global class state; tests that
# exercise ``setup()`` must not leak wrappers into other tests.
@pytest.fixture
def clean_cohere_methods():
from cohere.base_client import AsyncBaseCohere, BaseCohere
targets = [
(BaseCohere, "chat"),
(BaseCohere, "chat_stream"),
(BaseCohere, "embed"),
(BaseCohere, "rerank"),
(AsyncBaseCohere, "chat"),
(AsyncBaseCohere, "chat_stream"),
(AsyncBaseCohere, "embed"),
(AsyncBaseCohere, "rerank"),
]
try:
from cohere.v2.client import AsyncV2Client, V2Client
except ImportError:
pass
else:
for cls in (V2Client, AsyncV2Client):
for attr in ("chat", "chat_stream", "embed", "rerank"):
if hasattr(cls, attr):
targets.append((cls, attr))
try:
from cohere.audio.transcriptions.client import AsyncTranscriptionsClient, TranscriptionsClient
except ImportError:
pass
else:
for cls in (TranscriptionsClient, AsyncTranscriptionsClient):
if hasattr(cls, "create"):
targets.append((cls, "create"))
originals = [(cls, attr, inspect.getattr_static(cls, attr)) for cls, attr in targets]
# Also capture patch markers so we can clear them.
marker_attrs = set()
for patcher in (
ChatPatcher,
ChatStreamPatcher,
EmbedPatcher,
RerankPatcher,
AsyncChatPatcher,
AsyncChatStreamPatcher,
AsyncEmbedPatcher,
AsyncRerankPatcher,
V2ChatPatcher,
V2ChatStreamPatcher,
V2EmbedPatcher,
V2RerankPatcher,
AsyncV2ChatPatcher,
AsyncV2ChatStreamPatcher,
AsyncV2EmbedPatcher,
AsyncV2RerankPatcher,
TranscriptionsCreatePatcher,
AsyncTranscriptionsCreatePatcher,
):
marker_attrs.add(patcher.patch_marker_attr())
try:
yield
finally:
for cls, attr, original in originals:
setattr(cls, attr, original)
# Clear any patch markers that setup() may have added. wrapt can forward
# setattr from a FunctionWrapper onto the wrapped function, so the
# restored original may still carry the marker; clear it from both
# class and restored function.
for cls, _, original in originals:
for marker in marker_attrs:
if hasattr(cls, marker):
try:
delattr(cls, marker)
except AttributeError:
pass
if hasattr(original, marker):
try:
delattr(original, marker)
except AttributeError:
pass
# ---------------------------------------------------------------------------
# Unit / local tests (no network)
# ---------------------------------------------------------------------------
def test_wrap_cohere_returns_unsupported_unchanged(caplog):
invalid = object()
assert wrap_cohere(invalid) is invalid
invalid_dict = {"foo": "bar"}
assert wrap_cohere(invalid_dict) is invalid_dict
def test_wrap_cohere_is_idempotent():
client = _v1_client()
wrapped_once = wrap_cohere(client)
wrapped_twice = wrap_cohere(client)
assert wrapped_once is client
assert wrapped_twice is client
assert getattr(client, "__braintrust_cohere_traced__", False) is True
def test_audio_transcriptions_patchers_target_sdk_surface():
"""The audio transcription patchers must point at the Cohere SDK classes.
Regression guard for https://github.com/braintrustdata/braintrust-sdk-python/issues/327:
we must instrument both ``TranscriptionsClient.create`` and
``AsyncTranscriptionsClient.create`` on the ``cohere.audio.transcriptions``
surface introduced in cohere>=6.1.0.
"""
try:
import cohere.audio.transcriptions.client as transcriptions_module
except ImportError:
pytest.skip("cohere SDK does not expose audio.transcriptions")
assert TranscriptionsCreatePatcher.target_module == "cohere.audio.transcriptions.client"
assert TranscriptionsCreatePatcher.target_path == "TranscriptionsClient.create"
assert AsyncTranscriptionsCreatePatcher.target_module == "cohere.audio.transcriptions.client"
assert AsyncTranscriptionsCreatePatcher.target_path == "AsyncTranscriptionsClient.create"
assert hasattr(transcriptions_module.TranscriptionsClient, "create")
assert hasattr(transcriptions_module.AsyncTranscriptionsClient, "create")
# ---------------------------------------------------------------------------
# VCR-backed integration tests
# ---------------------------------------------------------------------------
@pytest.mark.vcr
def test_wrap_cohere_chat_v2_sync(memory_logger):
assert not memory_logger.pop()
client = wrap_cohere(_v2_client(require_methods=("chat",)))
start = time.time()
response = client.chat(
model=CHAT_MODEL,
messages=[{"role": "user", "content": "Say hi in one word."}],
max_tokens=10,
)
end = time.time()
# Provider behavior preserved.
assert response.message.role == "assistant"
spans = memory_logger.pop()
assert len(spans) == 1
span = spans[0]
assert span["context"]["span_origin"]["instrumentation"]["name"] == "cohere-auto"
assert span["span_attributes"]["name"] == "cohere.chat"
assert span["span_attributes"]["type"] == "llm"
assert span["metadata"]["provider"] == "cohere"
assert span["metadata"]["model"] == CHAT_MODEL
assert span["metadata"]["max_tokens"] == 10
assert span["input"] == [{"role": "user", "content": "Say hi in one word."}]
# Output is the normalized v2 message object.
assert isinstance(span["output"], dict)
assert span["output"]["role"] == "assistant"
assert_metrics_are_valid(span["metrics"], start, end)
@pytest.mark.vcr
def test_wrap_cohere_chat_v2_tool_call_spans(memory_logger):
if os.environ.get("BRAINTRUST_TEST_PACKAGE_VERSION") != "latest":
pytest.skip("v2 tool-call cassette is recorded for the latest Cohere SDK")
assert not memory_logger.pop()
client = wrap_cohere(_v2_client(require_methods=("chat",)))
response = client.chat(
model=CHAT_MODEL,
messages=[{"role": "user", "content": "Use the get_weather tool for Paris."}],
tools=[
{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the weather for a city.",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
},
}
],
tool_choice="REQUIRED",
max_tokens=64,
)
tool_calls = response.message.tool_calls
assert tool_calls
assert tool_calls[0].function.name == "get_weather"
spans = memory_logger.pop()
llm_spans = find_spans_by_type(spans, SpanTypeAttribute.LLM)
tool_spans = find_spans_by_type(spans, SpanTypeAttribute.TOOL)
assert len(llm_spans) == 1
assert len(tool_spans) == 1
tool_span = tool_spans[0]
assert tool_span["span_attributes"]["name"] == "tool: get_weather"
assert tool_span["span_parents"] == [llm_spans[0]["span_id"]]
assert tool_span["metadata"]["tool_call_id"] == tool_calls[0].id
assert tool_span["metadata"]["tool_type"] == "function"
assert "Paris" in str(tool_span["input"])
@pytest.mark.vcr
def test_wrap_cohere_chat_v1_tool_call_spans(memory_logger):
if os.environ.get("BRAINTRUST_TEST_PACKAGE_VERSION") != "latest":
pytest.skip("v1 tool-call cassette is recorded for the latest Cohere SDK")
assert not memory_logger.pop()
client = wrap_cohere(_v1_client())
response = client.chat(
model=CHAT_MODEL,
message="Use the get_weather tool for Paris.",
tools=[
{
"name": "get_weather",
"description": "Get the weather for a city.",
"parameter_definitions": {"city": {"description": "City name", "type": "str", "required": True}},
}
],
force_single_step=True,
max_tokens=64,
)
tool_calls = response.tool_calls
assert tool_calls
assert tool_calls[0].name == "get_weather"
spans = memory_logger.pop()
llm_spans = find_spans_by_type(spans, SpanTypeAttribute.LLM)
tool_spans = find_spans_by_type(spans, SpanTypeAttribute.TOOL)
assert len(llm_spans) == 1
assert len(tool_spans) == 1
tool_span = tool_spans[0]
assert tool_span["span_attributes"]["name"] == "tool: get_weather"
assert tool_span["span_parents"] == [llm_spans[0]["span_id"]]
assert "Paris" in str(tool_span["input"])
@pytest.mark.vcr
def test_wrap_cohere_chat_v1_sync(memory_logger):
assert not memory_logger.pop()
client = wrap_cohere(_v1_client())
start = time.time()
response = client.chat(
model=CHAT_MODEL,
message="Say hi in one word.",
max_tokens=10,
)
end = time.time()
assert isinstance(response.text, str)
spans = memory_logger.pop()
assert len(spans) == 1
span = spans[0]
assert span["span_attributes"]["name"] == "cohere.chat"
assert span["metadata"]["provider"] == "cohere"
assert span["metadata"]["model"] == CHAT_MODEL
assert span["input"] == "Say hi in one word."
assert isinstance(span["output"], str)
assert_metrics_are_valid(span["metrics"], start, end)
@pytest.mark.vcr
def test_wrap_cohere_embed_v1_sync(memory_logger):
assert not memory_logger.pop()
client = wrap_cohere(_v1_client())
start = time.time()
response = client.embed(
texts=["braintrust tracing"],
model=EMBED_MODEL,
input_type="search_document",
)
end = time.time()
# v1 embed returns a plain list-of-lists.
assert isinstance(response.embeddings, list)
assert len(response.embeddings) == 1
assert isinstance(response.embeddings[0], list)
assert response.embeddings[0]
spans = memory_logger.pop()
assert len(spans) == 1
span = spans[0]
assert span["span_attributes"]["name"] == "cohere.embed"
assert span["metadata"]["provider"] == "cohere"
assert span["metadata"]["model"] == EMBED_MODEL
assert span["metadata"]["input_type"] == "search_document"
assert span["input"] == ["braintrust tracing"]
assert span["output"]["embedding_count"] == 1
assert span["output"]["embedding_length"] > 0
metrics = span["metrics"]
assert metrics["duration"] >= 0
assert metrics["start"] <= metrics["end"]
assert start <= metrics["start"] <= metrics["end"] <= end
@pytest.mark.vcr
def test_wrap_cohere_embed_v2_sync(memory_logger):
assert not memory_logger.pop()
client = wrap_cohere(_v2_client(require_methods=("embed",)))
start = time.time()
response = client.embed(
texts=["braintrust tracing"],
model=EMBED_MODEL,
input_type="search_document",
embedding_types=["float"],
)
end = time.time()
# Provider behavior preserved.
assert hasattr(response, "embeddings")
spans = memory_logger.pop()
assert len(spans) == 1
span = spans[0]
assert span["span_attributes"]["name"] == "cohere.embed"
assert span["metadata"]["provider"] == "cohere"
assert span["metadata"]["model"] == EMBED_MODEL
assert span["metadata"]["input_type"] == "search_document"
assert span["input"] == ["braintrust tracing"]
assert span["output"]["embedding_count"] == 1
assert span["output"]["embedding_length"] > 0
metrics = span["metrics"]
assert metrics["duration"] >= 0
assert metrics["start"] <= metrics["end"]
assert start <= metrics["start"] <= metrics["end"] <= end
@pytest.mark.vcr
def test_wrap_cohere_rerank_sync(memory_logger):
assert not memory_logger.pop()
client = wrap_cohere(_v1_client())
response = client.rerank(
model=RERANK_MODEL,
query="capital of france",
documents=["Paris is in France", "Vienna is in Austria"],
top_n=2,
)
assert len(response.results) == 2
spans = memory_logger.pop()
assert len(spans) == 1
span = spans[0]
assert span["span_attributes"]["name"] == "cohere.rerank"
assert span["metadata"]["provider"] == "cohere"
assert span["metadata"]["model"] == RERANK_MODEL
assert span["metadata"]["top_n"] == 2
assert span["metadata"]["document_count"] == 2
assert span["input"] == {
"query": "capital of france",
"documents": ["Paris is in France", "Vienna is in Austria"],
}
assert isinstance(span["output"], list)
assert len(span["output"]) == 2
assert span["output"][0]["index"] in (0, 1)
assert isinstance(span["output"][0]["relevance_score"], float)
# Search units should show up as a dedicated metric.
assert span["metrics"].get("search_units") == 1
@pytest.mark.vcr
def test_wrap_cohere_chat_stream_v1_sync(memory_logger):
assert not memory_logger.pop()
client = wrap_cohere(_v1_client())
start = time.time()
events = []
for event in client.chat_stream(
model=CHAT_MODEL,
message="Say hi in one word.",
max_tokens=8,
):
events.append(event)
end = time.time()
assert events
event_types = [
event.get("event_type") if isinstance(event, dict) else getattr(event, "event_type", None) for event in events
]
assert "stream-start" in event_types
assert "stream-end" in event_types
assert "text-generation" in event_types
spans = memory_logger.pop()
assert len(spans) == 1
span = spans[0]
assert span["span_attributes"]["name"] == "cohere.chat_stream"
assert span["metadata"]["provider"] == "cohere"
assert span["metadata"]["model"] == CHAT_MODEL
assert span["input"] == "Say hi in one word."
assert span["metadata"].get("finish_reason") in {"COMPLETE", "MAX_TOKENS", "STOP_SEQUENCE"}
assert isinstance(span["output"], str) and span["output"]
metrics = span["metrics"]
assert metrics["start"] <= metrics["end"]
assert start <= metrics["start"] <= metrics["end"] <= end
assert metrics.get("prompt_tokens", 0) > 0
assert metrics.get("completion_tokens", 0) > 0
@pytest.mark.vcr
def test_wrap_cohere_chat_stream_v2_sync(memory_logger):
assert not memory_logger.pop()
client = wrap_cohere(_v2_client(require_methods=("chat_stream",)))
start = time.time()
events = []
for event in client.chat_stream(
model=CHAT_MODEL,
messages=[{"role": "user", "content": "Say hi in one word."}],
max_tokens=8,
):
events.append(event)
end = time.time()
assert events # provider still yielded events
spans = memory_logger.pop()
assert len(spans) == 1
span = spans[0]
assert span["span_attributes"]["name"] == "cohere.chat_stream"
assert span["metadata"]["provider"] == "cohere"
assert span["metadata"]["model"] == CHAT_MODEL
assert span["metadata"].get("finish_reason") in {"COMPLETE", "MAX_TOKENS", "STOP_SEQUENCE"}
output = span["output"]
# Either a v2-shaped dict or a v1-shaped string — both are acceptable
# aggregations as long as we captured *some* content.
if isinstance(output, dict):
content = output.get("content")
assert isinstance(content, str) and content
else:
assert isinstance(output, str) and output
metrics = span["metrics"]
assert metrics["start"] <= metrics["end"]
assert start <= metrics["start"] <= metrics["end"] <= end
assert metrics.get("prompt_tokens", 0) > 0
assert metrics.get("completion_tokens", 0) > 0
@pytest.mark.vcr
def test_wrap_cohere_chat_stream_v2_sync_context_manager(memory_logger):
assert not memory_logger.pop()
client = wrap_cohere(_v2_client(require_methods=("chat_stream",)))
start = time.time()
events = []
with client.chat_stream(
model=CHAT_MODEL,
messages=[{"role": "user", "content": "Say hi in one word."}],
max_tokens=8,
) as stream:
for event in stream:
events.append(event)
end = time.time()
assert events
spans = memory_logger.pop()
assert len(spans) == 1
span = spans[0]
assert span["span_attributes"]["name"] == "cohere.chat_stream"
assert span["metadata"]["provider"] == "cohere"
assert span["metadata"]["model"] == CHAT_MODEL
assert span["metrics"]["start"] <= span["metrics"]["end"]
assert start <= span["metrics"]["start"] <= span["metrics"]["end"] <= end
@pytest.mark.vcr
def test_wrap_cohere_chat_stream_v2_rag_citations(memory_logger):
if os.environ.get("BRAINTRUST_TEST_PACKAGE_VERSION") != "latest":
pytest.skip("v2 RAG citation cassette is recorded for the latest Cohere SDK")
assert not memory_logger.pop()
client = wrap_cohere(_v2_client(require_methods=("chat_stream",)))
documents = [
{
"data": {
"title": "Braintrust overview",
"snippet": "Braintrust is a platform for evaluating, logging, and improving AI applications.",
}
}
]
citation_options = {"mode": "fast"}
events = list(
client.chat_stream(
model=CHAT_MODEL,
messages=[{"role": "user", "content": "What is Braintrust? Cite the provided document."}],
documents=documents,
citation_options=citation_options,
max_tokens=80,
)
)
assert events
event_types = [getattr(event, "type", None) or getattr(event, "event_type", None) for event in events]
assert "citation-start" in event_types
spans = memory_logger.pop()
assert len(spans) == 1
span = spans[0]
assert span["metadata"]["documents"] == documents
assert span["metadata"]["citation_options"] == citation_options
output = span["output"]
assert isinstance(output, dict)
citations = output.get("citations")
assert isinstance(citations, list) and citations
assert citations[0].get("start") is not None
assert citations[0].get("end") is not None
assert citations[0].get("text")
assert citations[0].get("sources")
@pytest.mark.vcr
def test_wrap_cohere_chat_v1_async(memory_logger):
assert not memory_logger.pop()
async def _run():
client = wrap_cohere(_v1_async_client())
return await client.chat(
model=CHAT_MODEL,
message="Say hi in one word.",
max_tokens=10,
)
response = asyncio.run(_run())
assert isinstance(response.text, str)
spans = memory_logger.pop()
assert len(spans) == 1
span = spans[0]
assert span["span_attributes"]["name"] == "cohere.chat"
assert span["metadata"]["provider"] == "cohere"
assert span["metadata"]["model"] == CHAT_MODEL
assert isinstance(span["output"], str)
assert span["metrics"].get("prompt_tokens", 0) > 0
@pytest.mark.vcr
def test_wrap_cohere_audio_transcription_sync(memory_logger):
pytest.importorskip("cohere.audio.transcriptions.client")
assert not memory_logger.pop()
client = wrap_cohere(_v1_client())
start = time.time()
with open(TEST_AUDIO_FILE, "rb") as file_obj:
response = client.audio.transcriptions.create(
model=TRANSCRIBE_MODEL,
language="en",
file=(TEST_AUDIO_FILE.name, file_obj, "audio/wav"),
temperature=0.0,
)
end = time.time()
# Provider behavior preserved.
assert isinstance(response.text, str)
assert response.text # non-empty
spans = memory_logger.pop()
assert len(spans) == 1
span = spans[0]
assert span["span_attributes"]["name"] == "cohere.audio.transcriptions.create"
assert span["span_attributes"]["type"] == "llm"
assert span["metadata"]["provider"] == "cohere"
assert span["metadata"]["model"] == TRANSCRIBE_MODEL
assert span["metadata"]["language"] == "en"
assert span["metadata"]["temperature"] == 0.0
# Input carries the audio file as an Attachment.
file_attachment = span["input"]["file"]
assert isinstance(file_attachment, Attachment)
assert file_attachment.reference["filename"] == TEST_AUDIO_FILE.name
assert file_attachment.reference["content_type"] == "audio/wav"
# Output is the transcribed text.
assert span["output"] == response.text
# Cohere's transcription response does not expose token counts, so we
# only assert the timing metrics we always record.
metrics = span["metrics"]
assert start <= metrics["start"] <= metrics["end"] <= end
assert metrics["duration"] >= 0
@pytest.mark.vcr
def test_wrap_cohere_audio_transcription_async(memory_logger):
pytest.importorskip("cohere.audio.transcriptions.client")
assert not memory_logger.pop()
async def _run():
client = wrap_cohere(_v1_async_client())
with open(TEST_AUDIO_FILE, "rb") as file_obj:
return await client.audio.transcriptions.create(
model=TRANSCRIBE_MODEL,
language="en",
file=(TEST_AUDIO_FILE.name, file_obj, "audio/wav"),
)
response = asyncio.run(_run())
assert isinstance(response.text, str)
spans = memory_logger.pop()
assert len(spans) == 1
span = spans[0]
assert span["span_attributes"]["name"] == "cohere.audio.transcriptions.create"
assert span["metadata"]["provider"] == "cohere"
assert span["metadata"]["model"] == TRANSCRIBE_MODEL
assert span["metadata"]["language"] == "en"
file_attachment = span["input"]["file"]
assert isinstance(file_attachment, Attachment)
assert file_attachment.reference["filename"] == TEST_AUDIO_FILE.name
assert span["output"] == response.text
@pytest.mark.vcr
def test_cohere_integration_setup_patches_audio_transcriptions(memory_logger, clean_cohere_methods):
"""``CohereIntegration.setup()`` must wire up audio transcription tracing."""
pytest.importorskip("cohere.audio.transcriptions.client")
assert not memory_logger.pop()
assert CohereIntegration.setup() is True
# Second call is a no-op but still reports success.
assert CohereIntegration.setup() is True
client = _v1_client() # NOT manually wrapped
with open(TEST_AUDIO_FILE, "rb") as file_obj:
response = client.audio.transcriptions.create(
model=TRANSCRIBE_MODEL,
language="en",
file=(TEST_AUDIO_FILE.name, file_obj, "audio/wav"),
temperature=0.0,
)
assert isinstance(response.text, str)
spans = memory_logger.pop()
assert len(spans) == 1
assert spans[0]["span_attributes"]["name"] == "cohere.audio.transcriptions.create"
assert spans[0]["metadata"]["provider"] == "cohere"
assert spans[0]["metadata"]["model"] == TRANSCRIBE_MODEL
class TestAutoInstrumentCohere:
def test_auto_instrument_cohere(self):
verify_autoinstrument_script("test_auto_cohere.py")