2626 BaseModel ,
2727 ConfigDict ,
2828 Field ,
29+ FieldSerializationInfo ,
2930 StringConstraints ,
3031 computed_field ,
32+ field_serializer ,
3133 field_validator ,
3234 model_validator ,
3335 validate_call ,
3840from docling_core .search .package import VERSION_PATTERN
3941from docling_core .types .base import _JSON_POINTER_REGEX
4042from docling_core .types .doc import BoundingBox , Size
41- from docling_core .types .doc .base import CoordOrigin , ImageRefMode
43+ from docling_core .types .doc .base import (
44+ _CTX_COORD_PREC ,
45+ CoordOrigin ,
46+ ImageRefMode ,
47+ _serialize_precision ,
48+ )
4249from docling_core .types .doc .labels import (
4350 CodeLanguageLabel ,
4451 DocItemLabel ,
8592 ]
8693)
8794
95+ _CTX_CONFID_PREC = "confid_prec"
96+
8897
8998class BaseAnnotation (BaseModel ):
9099 """Base class for all annotation types."""
@@ -98,6 +107,10 @@ class PictureClassificationClass(BaseModel):
98107 class_name : str
99108 confidence : float
100109
110+ @field_serializer ("confidence" )
111+ def _serialize (self , value : float , info : FieldSerializationInfo ) -> float :
112+ return _serialize_precision (value , info , _CTX_CONFID_PREC )
113+
101114
102115class PictureClassificationData (BaseAnnotation ):
103116 """PictureClassificationData."""
@@ -125,6 +138,10 @@ class PictureMoleculeData(BaseAnnotation):
125138 segmentation : List [Tuple [float , float ]]
126139 provenance : str
127140
141+ @field_serializer ("confidence" )
142+ def _serialize (self , value : float , info : FieldSerializationInfo ) -> float :
143+ return _serialize_precision (value , info , _CTX_CONFID_PREC )
144+
128145
129146class MiscAnnotation (BaseAnnotation ):
130147 """MiscAnnotation."""
@@ -3048,6 +3065,8 @@ def save_as_json(
30483065 artifacts_dir : Optional [Path ] = None ,
30493066 image_mode : ImageRefMode = ImageRefMode .EMBEDDED ,
30503067 indent : int = 2 ,
3068+ coord_precision : Optional [int ] = None ,
3069+ confid_precision : Optional [int ] = None ,
30513070 ):
30523071 """Save as json."""
30533072 if isinstance (filename , str ):
@@ -3061,7 +3080,9 @@ def save_as_json(
30613080 artifacts_dir , image_mode , reference_path = reference_path
30623081 )
30633082
3064- out = new_doc .export_to_dict ()
3083+ out = new_doc .export_to_dict (
3084+ coord_precision = coord_precision , confid_precision = confid_precision
3085+ )
30653086 with open (filename , "w" , encoding = "utf-8" ) as fw :
30663087 json .dump (out , fw , indent = indent )
30673088
@@ -3087,6 +3108,8 @@ def save_as_yaml(
30873108 artifacts_dir : Optional [Path ] = None ,
30883109 image_mode : ImageRefMode = ImageRefMode .EMBEDDED ,
30893110 default_flow_style : bool = False ,
3111+ coord_precision : Optional [int ] = None ,
3112+ confid_precision : Optional [int ] = None ,
30903113 ):
30913114 """Save as yaml."""
30923115 if isinstance (filename , str ):
@@ -3100,7 +3123,9 @@ def save_as_yaml(
31003123 artifacts_dir , image_mode , reference_path = reference_path
31013124 )
31023125
3103- out = new_doc .export_to_dict ()
3126+ out = new_doc .export_to_dict (
3127+ coord_precision = coord_precision , confid_precision = confid_precision
3128+ )
31043129 with open (filename , "w" , encoding = "utf-8" ) as fw :
31053130 yaml .dump (out , fw , default_flow_style = default_flow_style )
31063131
@@ -3125,9 +3150,18 @@ def export_to_dict(
31253150 mode : str = "json" ,
31263151 by_alias : bool = True ,
31273152 exclude_none : bool = True ,
3153+ coord_precision : Optional [int ] = None ,
3154+ confid_precision : Optional [int ] = None ,
31283155 ) -> Dict [str , Any ]:
31293156 """Export to dict."""
3130- out = self .model_dump (mode = mode , by_alias = by_alias , exclude_none = exclude_none )
3157+ context = {}
3158+ if coord_precision is not None :
3159+ context [_CTX_COORD_PREC ] = coord_precision
3160+ if confid_precision is not None :
3161+ context [_CTX_CONFID_PREC ] = confid_precision
3162+ out = self .model_dump (
3163+ mode = mode , by_alias = by_alias , exclude_none = exclude_none , context = context
3164+ )
31313165
31323166 return out
31333167
0 commit comments