1
- from typing import get_args , get_origin , List
1
+ from pandas import DataFrame
2
+ from pydantic import BaseModel , ConfigDict , create_model
2
3
3
- from pydantic import BaseModel , create_model , ConfigDict
4
- from haystack .dataclasses import Document
5
-
6
-
7
- class HaystackDocument (BaseModel ):
8
- id : str
9
- content : str
4
+ from hayhooks .server .utils .create_valid_type import handle_unsupported_types
10
5
11
6
12
7
class PipelineDefinition (BaseModel ):
@@ -29,13 +24,16 @@ def get_request_model(pipeline_name: str, pipeline_inputs):
29
24
config = ConfigDict (arbitrary_types_allowed = True )
30
25
31
26
for component_name , inputs in pipeline_inputs .items ():
32
-
33
27
component_model = {}
34
28
for name , typedef in inputs .items ():
35
- component_model [name ] = (typedef ["type" ], typedef .get ("default_value" , ...))
36
- request_model [component_name ] = (create_model ('ComponentParams' , ** component_model , __config__ = config ), ...)
29
+ input_type = handle_unsupported_types (typedef ["type" ], {DataFrame : dict })
30
+ component_model [name ] = (
31
+ input_type ,
32
+ typedef .get ("default_value" , ...),
33
+ )
34
+ request_model [component_name ] = (create_model ("ComponentParams" , ** component_model , __config__ = config ), ...)
37
35
38
- return create_model (f' { pipeline_name .capitalize ()} RunRequest' , ** request_model , __config__ = config )
36
+ return create_model (f" { pipeline_name .capitalize ()} RunRequest" , ** request_model , __config__ = config )
39
37
40
38
41
39
def get_response_model (pipeline_name : str , pipeline_outputs ):
@@ -49,44 +47,32 @@ def get_response_model(pipeline_name: str, pipeline_outputs):
49
47
"""
50
48
response_model = {}
51
49
config = ConfigDict (arbitrary_types_allowed = True )
52
-
53
50
for component_name , outputs in pipeline_outputs .items ():
54
51
component_model = {}
55
52
for name , typedef in outputs .items ():
56
53
output_type = typedef ["type" ]
57
- if get_origin (output_type ) == list and get_args (output_type )[0 ] == Document :
58
- component_model [name ] = (List [HaystackDocument ], ...)
59
- else :
60
- component_model [name ] = (typedef ["type" ], ...)
61
- response_model [component_name ] = (create_model ('ComponentParams' , ** component_model , __config__ = config ), ...)
54
+ component_model [name ] = (handle_unsupported_types (output_type , {DataFrame : dict }), ...)
55
+ response_model [component_name ] = (create_model ("ComponentParams" , ** component_model , __config__ = config ), ...)
62
56
63
- return create_model (f' { pipeline_name .capitalize ()} RunResponse' , ** response_model , __config__ = config )
57
+ return create_model (f" { pipeline_name .capitalize ()} RunResponse" , ** response_model , __config__ = config )
64
58
65
59
66
60
def convert_component_output (component_output ):
67
61
"""
62
+ Converts outputs from a component as a dict so that it can be validated against response model
63
+
68
64
Component output has this form:
69
65
70
66
"documents":[
71
67
{"id":"818170...", "content":"RapidAPI for Mac is a full-featured HTTP client."}
72
68
]
73
69
74
- We inspect the output and convert haystack.Document into the HaystackDocument pydantic model as needed
75
70
"""
76
71
result = {}
77
72
for output_name , data in component_output .items ():
78
- # Empty containers, None values, empty strings and the likes: do nothing
79
- if not data :
80
- result [output_name ] = data
81
-
82
- # Output contains a list of Document
83
- if type (data ) is list and type (data [0 ]) is Document :
84
- result [output_name ] = [HaystackDocument (id = d .id , content = d .content ) for d in data ]
85
- # Output is a single Document
86
- elif type (data ) is Document :
87
- result [output_name ] = HaystackDocument (id = data .id , content = data .content or "" )
88
- # Any other type: do nothing
73
+ get_value = lambda data : data .to_dict ()["init_parameters" ] if hasattr (data , "to_dict" ) else data
74
+ if type (data ) is list :
75
+ result [output_name ] = [get_value (d ) for d in data ]
89
76
else :
90
- result [output_name ] = data
91
-
77
+ result [output_name ] = get_value (data )
92
78
return result
0 commit comments