Skip to content

Commit 1d5ac69

Browse files
committed
fix: prevent type wrappers for primitive types
1 parent c5526c8 commit 1d5ac69

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

logfire/_internal/json_schema.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,13 @@ def _pydantic_root_model_schema(obj: Any, seen: set[int]) -> JsonDict:
293293

294294
if isinstance(root, type(None)):
295295
return {'type': 'null'}
296+
297+
schema: JsonDict = {}
298+
296299
# return a complex schema to ensure JSON parsing for simple objects inside RootModel since they get an
297300
# extra layer of JSON encoding and to handle subclass representations correctly
298-
elif isinstance(root, (str, bool, int, float)):
301+
primitive_types = (str, bool, int, float)
302+
if isinstance(root, primitive_types):
299303
datatype = None
300304
if isinstance(root, str):
301305
type_ = 'string'
@@ -309,11 +313,11 @@ def _pydantic_root_model_schema(obj: Any, seen: set[int]) -> JsonDict:
309313
type_ = 'number'
310314
datatype = 'number'
311315

312-
return {
313-
'type': type_,
314-
'x-python-datatype': datatype,
315-
'title': root.__class__.__name__,
316-
}
316+
schema = {'type': type_, 'x-python-datatype': datatype}
317+
if root.__class__ not in primitive_types:
318+
schema['title'] = root.__class__.__name__
319+
320+
return schema
317321

318322
return create_json_schema(obj.root, seen) # type: ignore
319323

tests/test_console_exporter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,11 +1094,11 @@ class Order(IntEnum):
10941094
'│ with_model=Model(',
10951095
"│ name='with_model',",
10961096
'│ )',
1097-
"│ with_str=str('with_str')",
1097+
"│ with_str='with_str'",
10981098
"│ with_str_inner='with_str'",
1099-
'│ with_int=int(-150)',
1099+
'│ with_int=-150',
11001100
'│ with_int_inner=-150',
1101-
'│ with_float=float(2.0)',
1101+
'│ with_float=2.0',
11021102
'│ with_float_inner=2.0',
11031103
'│ with_bool=False',
11041104
'│ with_bool_inner=False',

tests/test_json_args.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1463,7 +1463,7 @@ class Order(IntEnum):
14631463
'with_color_inner': '"RED"',
14641464
'with_order': '1',
14651465
'with_order_inner': '1',
1466-
'logfire.json_schema': '{"type":"object","properties":{"with_model":{"type":"object","title":"Model","x-python-datatype":"PydanticModel"},"with_str":{"type":"string","x-python-datatype":"string","title":"str"},"with_str_inner":{},"with_int":{"type":"integer","x-python-datatype":"number","title":"int"},"with_int_inner":{},"with_float":{"type":"number","x-python-datatype":"number","title":"float"},"with_float_inner":{},"with_bool":{"type":"boolean","x-python-datatype":null,"title":"bool"},"with_bool_inner":{},"with_none":{"type":"null"},"with_none_inner":{"type":"null"},"with_color":{"type":"string","x-python-datatype":"string","title":"Color"},"with_color_inner":{"type":"string","title":"Color","x-python-datatype":"Enum","enum":["RED"]},"with_order":{"type":"integer","x-python-datatype":"number","title":"Order"},"with_order_inner":{"type":"integer","title":"Order","x-python-datatype":"Enum","enum":[1]}}}',
1466+
'logfire.json_schema': '{"type":"object","properties":{"with_model":{"type":"object","title":"Model","x-python-datatype":"PydanticModel"},"with_str":{"type":"string","x-python-datatype":"string"},"with_str_inner":{},"with_int":{"type":"integer","x-python-datatype":"number"},"with_int_inner":{},"with_float":{"type":"number","x-python-datatype":"number"},"with_float_inner":{},"with_bool":{"type":"boolean","x-python-datatype":null},"with_bool_inner":{},"with_none":{"type":"null"},"with_none_inner":{"type":"null"},"with_color":{"type":"string","x-python-datatype":"string","title":"Color"},"with_color_inner":{"type":"string","title":"Color","x-python-datatype":"Enum","enum":["RED"]},"with_order":{"type":"integer","x-python-datatype":"number","title":"Order"},"with_order_inner":{"type":"integer","title":"Order","x-python-datatype":"Enum","enum":[1]}}}',
14671467
},
14681468
}
14691469
]

0 commit comments

Comments
 (0)