-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_parameters.py
More file actions
537 lines (455 loc) · 15.2 KB
/
test_parameters.py
File metadata and controls
537 lines (455 loc) · 15.2 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
import importlib.util
import pytest
from braintrust.parameters import (
RemoteEvalParameters,
parameters_to_json_schema,
serialize_eval_parameters,
validate_parameters,
)
HAS_PYDANTIC = importlib.util.find_spec("pydantic") is not None
def _contains_json_schema_ref(node):
if isinstance(node, dict):
if "$ref" in node or "$defs" in node or "definitions" in node:
return True
return any(_contains_json_schema_ref(value) for value in node.values())
if isinstance(node, list):
return any(_contains_json_schema_ref(value) for value in node)
return False
@pytest.mark.skipif(not HAS_PYDANTIC, reason="pydantic not installed")
def test_validate_local_parameters_with_prompt_and_model_defaults():
from pydantic import BaseModel
class PrefixParam(BaseModel):
value: str = "hello"
result = validate_parameters(
{},
{
"prefix": PrefixParam,
"model": {
"type": "model",
"default": "gpt-5-mini",
},
"main": {
"type": "prompt",
"default": {
"prompt": {
"type": "chat",
"messages": [{"role": "user", "content": "{{input}}"}],
},
"options": {
"model": "gpt-5-mini",
},
},
},
},
)
assert result["prefix"] == "hello"
assert result["model"] == "gpt-5-mini"
assert hasattr(result["main"], "build")
def test_validate_remote_parameters_merges_saved_data_and_runtime_overrides():
parameters = RemoteEvalParameters(
id="params-123",
project_id="project-123",
name="Saved parameters",
slug="saved-parameters",
version="v1",
schema={
"type": "object",
"properties": {
"prefix": {
"type": "string",
"default": "saved-prefix",
},
"model": {
"type": "string",
"x-bt-type": "model",
"default": "gpt-5-mini",
},
},
"additionalProperties": True,
},
data={"prefix": "saved-prefix"},
)
result = validate_parameters({"model": "gpt-5-nano"}, parameters)
assert result == {
"prefix": "saved-prefix",
"model": "gpt-5-nano",
}
def test_validate_remote_parameters_rehydrates_prompt_values():
parameters = RemoteEvalParameters(
id="params-123",
project_id="project-123",
name="Saved parameters",
slug="saved-parameters",
version="v1",
schema={
"type": "object",
"properties": {
"main": {
"type": "object",
"x-bt-type": "prompt",
},
},
"additionalProperties": True,
},
data={
"main": {
"prompt": {
"type": "chat",
"messages": [{"role": "user", "content": "{{input}}"}],
},
"options": {
"model": "gpt-5-mini",
},
},
},
)
result = validate_parameters({}, parameters)
assert hasattr(result["main"], "build")
built = result["main"].build(input="test input")
assert built["messages"] == [{"role": "user", "content": "test input"}]
assert built["model"] == "gpt-5-mini"
def test_validate_remote_parameters_allows_prompt_overrides():
parameters = RemoteEvalParameters(
id="params-123",
project_id="project-123",
name="Saved parameters",
slug="saved-parameters",
version="v1",
schema={
"type": "object",
"properties": {
"main": {
"type": "object",
"x-bt-type": "prompt",
},
},
"additionalProperties": True,
},
data={
"main": {
"prompt": {
"type": "chat",
"messages": [{"role": "user", "content": "{{input}}"}],
},
"options": {
"model": "gpt-5-mini",
},
},
},
)
result = validate_parameters(
{
"main": {
"prompt": {
"type": "chat",
"messages": [{"role": "user", "content": "override {{input}}"}],
},
"options": {
"model": "gpt-5-nano",
},
}
},
parameters,
)
assert hasattr(result["main"], "build")
built = result["main"].build(input="test input")
assert built["messages"] == [{"role": "user", "content": "override test input"}]
assert built["model"] == "gpt-5-nano"
@pytest.mark.skipif(not HAS_PYDANTIC, reason="pydantic not installed")
def test_parameters_to_json_schema_uses_scalar_schema_for_single_value_models():
from pydantic import BaseModel
class PrefixParam(BaseModel):
value: str = "hello"
schema = parameters_to_json_schema({"prefix": PrefixParam})
assert schema["properties"]["prefix"] == {
"type": "string",
"default": "hello",
"title": "Value",
}
@pytest.mark.skipif(not HAS_PYDANTIC, reason="pydantic not installed")
def test_parameters_to_json_schema_keeps_complex_single_value_models_self_contained():
from pydantic import BaseModel, Field
class ComplexValue(BaseModel):
a: int = Field(default=1)
b: list[int] = Field(default=[2, 3])
class ComplexParameter(BaseModel):
value: ComplexValue = Field(
default=ComplexValue(),
description="Complex example parameter",
)
schema = parameters_to_json_schema({"complex": ComplexParameter})
assert schema["properties"]["complex"] == {
"type": "object",
"properties": {
"a": {
"default": 1,
"title": "A",
"type": "integer",
},
"b": {
"default": [2, 3],
"items": {
"type": "integer",
},
"title": "B",
"type": "array",
},
},
"default": {
"a": 1,
"b": [2, 3],
},
"description": "Complex example parameter",
"title": "ComplexValue",
}
@pytest.mark.skipif(not HAS_PYDANTIC, reason="pydantic not installed")
def test_serialize_eval_parameters_does_not_emit_dangling_ref_for_complex_single_value_models():
from pydantic import BaseModel, Field
class ComplexValue(BaseModel):
a: int = Field(default=1)
b: list[int] = Field(default=[2, 3])
class ComplexParameter(BaseModel):
value: ComplexValue = Field(
default=ComplexValue(),
description="Complex example parameter",
)
serialized = serialize_eval_parameters({"complex": ComplexParameter})
assert serialized["complex"] == {
"type": "data",
"schema": {
"type": "object",
"properties": {
"a": {
"default": 1,
"title": "A",
"type": "integer",
},
"b": {
"default": [2, 3],
"items": {
"type": "integer",
},
"title": "B",
"type": "array",
},
},
"default": {
"a": 1,
"b": [2, 3],
},
"description": "Complex example parameter",
"title": "ComplexValue",
},
"default": {
"a": 1,
"b": [2, 3],
},
"description": "Complex example parameter",
}
@pytest.mark.skipif(not HAS_PYDANTIC, reason="pydantic not installed")
def test_parameters_to_json_schema_keeps_array_of_objects_single_value_models_self_contained():
from pydantic import BaseModel, Field
class ComplexItem(BaseModel):
a: int = Field(default=1)
b: str = Field(default="x")
class ComplexParameter(BaseModel):
value: list[ComplexItem] = Field(
default=[ComplexItem()],
description="Array example parameter",
)
schema = parameters_to_json_schema({"complex_array": ComplexParameter})
assert schema["properties"]["complex_array"] == {
"type": "array",
"items": {
"type": "object",
"properties": {
"a": {
"default": 1,
"title": "A",
"type": "integer",
},
"b": {
"default": "x",
"title": "B",
"type": "string",
},
},
"title": "ComplexItem",
},
"default": [
{
"a": 1,
"b": "x",
}
],
"description": "Array example parameter",
"title": "Value",
}
assert not _contains_json_schema_ref(schema["properties"]["complex_array"])
@pytest.mark.skipif(not HAS_PYDANTIC, reason="pydantic not installed")
def test_parameters_to_json_schema_inlines_refs_for_multi_field_models():
from pydantic import BaseModel, Field
class Address(BaseModel):
street: str = Field(default="Main")
zip_code: int = Field(default=12345)
class ComplexParameter(BaseModel):
address: Address = Field(default=Address())
enabled: bool = Field(default=True)
schema = parameters_to_json_schema({"complex": ComplexParameter})
assert not _contains_json_schema_ref(schema["properties"]["complex"])
assert schema["properties"]["complex"]["properties"]["address"] == {
"type": "object",
"properties": {
"street": {
"default": "Main",
"title": "Street",
"type": "string",
},
"zip_code": {
"default": 12345,
"title": "Zip Code",
"type": "integer",
},
},
"default": {
"street": "Main",
"zip_code": 12345,
},
"title": "Address",
}
def test_parameters_to_json_schema_inlines_legacy_definitions_refs():
class _FakeField:
required = False
class LegacyParameter:
__fields__ = {"value": _FakeField()}
@classmethod
def parse_obj(cls, value):
return value
@classmethod
def schema(cls):
return {
"type": "object",
"properties": {
"value": {
"$ref": "#/definitions/ComplexValue",
"description": "Legacy example parameter",
"default": {"a": 1},
},
},
"definitions": {
"ComplexValue": {
"type": "object",
"properties": {
"a": {
"type": "integer",
"title": "A",
"default": 1,
},
},
"title": "ComplexValue",
},
},
}
schema = parameters_to_json_schema({"legacy": LegacyParameter})
assert schema["properties"]["legacy"] == {
"type": "object",
"properties": {
"a": {
"type": "integer",
"title": "A",
"default": 1,
},
},
"title": "ComplexValue",
"description": "Legacy example parameter",
"default": {"a": 1},
}
assert not _contains_json_schema_ref(schema["properties"]["legacy"])
def test_parameters_to_json_schema_raises_for_cyclic_local_refs():
class _FakeField:
required = False
class CyclicParameter:
__fields__ = {"value": _FakeField()}
@classmethod
def parse_obj(cls, value):
return value
@classmethod
def schema(cls):
return {
"type": "object",
"properties": {
"value": {
"$ref": "#/definitions/Node",
},
},
"definitions": {
"Node": {
"type": "object",
"properties": {
"child": {
"$ref": "#/definitions/Node",
},
},
},
},
}
with pytest.raises(ValueError, match="Cyclic JSON schema ref"):
parameters_to_json_schema({"cyclic": CyclicParameter})
@pytest.mark.skipif(not HAS_PYDANTIC, reason="pydantic not installed")
def test_parameters_to_json_schema_marks_prompt_and_model_without_defaults_required():
schema = parameters_to_json_schema(
{
"prompt_required": {"type": "prompt"},
"prompt_optional": {
"type": "prompt",
"default": {
"prompt": {
"type": "chat",
"messages": [{"role": "user", "content": "{{input}}"}],
},
"options": {"model": "gpt-5-mini"},
},
},
"model_required": {"type": "model"},
"model_optional": {"type": "model", "default": "gpt-5-mini"},
}
)
assert schema["required"] == ["prompt_required", "model_required"]
@pytest.mark.skipif(not HAS_PYDANTIC, reason="pydantic not installed")
def test_parameters_to_json_schema_marks_single_value_models_required_from_value_field():
from pydantic import BaseModel
class RequiredScalarParam(BaseModel):
value: int
class OptionalScalarParam(BaseModel):
value: int = 3
schema = parameters_to_json_schema(
{
"required_scalar": RequiredScalarParam,
"optional_scalar": OptionalScalarParam,
}
)
assert schema["required"] == ["required_scalar"]
@pytest.mark.skipif(not HAS_PYDANTIC, reason="pydantic not installed")
def test_parameters_to_json_schema_does_not_mark_multi_field_models_required():
from pydantic import BaseModel
class RequiredObjectParam(BaseModel):
x: int
y: int = 2
class OptionalObjectParam(BaseModel):
x: int = 1
y: int = 2
schema = parameters_to_json_schema(
{
"required_object": RequiredObjectParam,
"optional_object": OptionalObjectParam,
}
)
assert "required" not in schema
@pytest.mark.skipif(not HAS_PYDANTIC, reason="pydantic not installed")
def test_parameters_to_json_schema_does_not_mark_passthrough_values_required():
schema = parameters_to_json_schema(
{
"passthrough": None,
}
)
assert "required" not in schema