2323from docling_core .transforms .serializer .common import (
2424 CommonParams ,
2525 DocSerializer ,
26+ _should_use_legacy_annotations ,
2627 create_ser_result ,
2728)
2829from docling_core .types .doc .base import BoundingBox
@@ -236,18 +237,25 @@ def serialize(
236237
237238 # handle classification data
238239 predicted_class : Optional [str ] = None
239- if item .meta and item .meta .classification :
240- predicted_class = (
241- item .meta .classification .get_main_prediction ().class_name
242- )
243- elif (
244- classifications := [
240+ if item .meta :
241+ if item .meta .classification :
242+ predicted_class = (
243+ item .meta .classification .get_main_prediction ().class_name
244+ )
245+ elif _should_use_legacy_annotations (
246+ params = params ,
247+ item = item ,
248+ kind = PictureClassificationData .model_fields ["kind" ].default ,
249+ ):
250+ if classifications := [
245251 ann
246252 for ann in item .annotations
247253 if isinstance (ann , PictureClassificationData )
248- ]
249- ) and classifications [0 ].predicted_classes :
250- predicted_class = classifications [0 ].predicted_classes [0 ].class_name
254+ ]:
255+ if classifications [0 ].predicted_classes :
256+ predicted_class = (
257+ classifications [0 ].predicted_classes [0 ].class_name
258+ )
251259 if predicted_class :
252260 body += DocumentToken .get_picture_classification_token (predicted_class )
253261 if predicted_class in [
@@ -263,25 +271,39 @@ def serialize(
263271
264272 # handle molecule data
265273 smi : Optional [str ] = None
266- if item .meta and item .meta .molecule :
267- smi = item .meta .molecule .smi
268- elif smiles_annotations := [
269- ann for ann in item .annotations if isinstance (ann , PictureMoleculeData )
270- ]:
271- smi = smiles_annotations [0 ].smi
274+ if item .meta :
275+ if item .meta .molecule :
276+ smi = item .meta .molecule .smi
277+ elif _should_use_legacy_annotations (
278+ params = params ,
279+ item = item ,
280+ kind = PictureMoleculeData .model_fields ["kind" ].default ,
281+ ):
282+ if smiles_annotations := [
283+ ann
284+ for ann in item .annotations
285+ if isinstance (ann , PictureMoleculeData )
286+ ]:
287+ smi = smiles_annotations [0 ].smi
272288 if smi :
273289 body += _wrap (text = smi , wrap_tag = DocumentToken .SMILES .value )
274290
275291 # handle tabular chart data
276292 chart_data : Optional [TableData ] = None
277- if item .meta and item .meta .tabular_chart :
278- chart_data = item .meta .tabular_chart .chart_data
279- elif tabular_chart_annotations := [
280- ann
281- for ann in item .annotations
282- if isinstance (ann , PictureTabularChartData )
283- ]:
284- chart_data = tabular_chart_annotations [0 ].chart_data
293+ if item .meta :
294+ if item .meta .tabular_chart :
295+ chart_data = item .meta .tabular_chart .chart_data
296+ elif _should_use_legacy_annotations (
297+ params = params ,
298+ item = item ,
299+ kind = PictureTabularChartData .model_fields ["kind" ].default ,
300+ ):
301+ if tabular_chart_annotations := [
302+ ann
303+ for ann in item .annotations
304+ if isinstance (ann , PictureTabularChartData )
305+ ]:
306+ chart_data = tabular_chart_annotations [0 ].chart_data
285307 if chart_data and chart_data .table_cells :
286308 temp_doc = DoclingDocument (name = "temp" )
287309 temp_table = temp_doc .add_table (data = chart_data )
0 commit comments