1- from typing import Any , Callable , Dict , List , Optional , Type , Union
1+ from collections .abc import Callable
2+ from typing import Any
23
34import dspy
45from haystack import component , default_from_dict , default_to_dict
@@ -75,14 +76,14 @@ class QASignature(dspy.Signature):
7576
7677 def __init__ (
7778 self ,
78- signature : Union [ str , Type [dspy .Signature ] ],
79+ signature : str | type [dspy .Signature ],
7980 model : str = "openai/gpt-5-mini" ,
8081 api_key : Secret = Secret .from_env_var ("OPENAI_API_KEY" ),
8182 module_type : str = "ChainOfThought" ,
8283 output_field : str = "answer" ,
83- generation_kwargs : Optional [ Dict [ str , Any ]] = None ,
84- input_mapping : Optional [ Dict [ str , str ]] = None ,
85- streaming_callback : Optional [ Callable ] = None ,
84+ generation_kwargs : dict [ str , Any ] | None = None ,
85+ input_mapping : dict [ str , str ] | None = None ,
86+ streaming_callback : Callable | None = None ,
8687 ):
8788 """
8889 Initialize the DSPyChatGenerator.
@@ -119,7 +120,7 @@ def __init__(
119120 module_class = get_dspy_module_class (self .module_type )
120121 self ._module = module_class (self .signature )
121122
122- def _build_dspy_inputs (self , prompt : str , ** kwargs ) -> Dict [str , Any ]:
123+ def _build_dspy_inputs (self , prompt : str , ** kwargs ) -> dict [str , Any ]:
123124 """Build the input dict for the DSPy module call."""
124125 if self .input_mapping :
125126 dspy_inputs = {}
@@ -139,15 +140,15 @@ def _build_dspy_inputs(self, prompt: str, **kwargs) -> Dict[str, Any]:
139140
140141 return dspy_inputs
141142
142- def _get_input_field_names (self ) -> List [str ]:
143+ def _get_input_field_names (self ) -> list [str ]:
143144 """Get input field names from the signature."""
144145 if isinstance (self .signature , str ):
145146 input_part = self .signature .split ("->" )[0 ].strip ()
146147 return [f .strip () for f in input_part .split ("," )]
147148 return list (self .signature .input_fields .keys ())
148149
149150 @staticmethod
150- def _extract_last_user_message (messages : List [ChatMessage ]) -> str :
151+ def _extract_last_user_message (messages : list [ChatMessage ]) -> str :
151152 """Extract the text of the last user message from a list of chat messages."""
152153 for msg in reversed (messages ):
153154 if msg .role == ChatRole .USER :
@@ -162,9 +163,9 @@ def _signature_to_string(self) -> str:
162163 output_names = list (self .signature .output_fields .keys ())
163164 return ", " .join (input_names ) + " -> " + ", " .join (output_names )
164165
165- def to_dict (self ) -> Dict [str , Any ]:
166+ def to_dict (self ) -> dict [str , Any ]:
166167 """Serialize this component to a dictionary."""
167- kwargs : Dict [str , Any ] = {
168+ kwargs : dict [str , Any ] = {
168169 "signature" : self ._signature_to_string (),
169170 "model" : self .model ,
170171 "module_type" : self .module_type ,
@@ -179,19 +180,19 @@ def to_dict(self) -> Dict[str, Any]:
179180 return default_to_dict (self , ** kwargs )
180181
181182 @classmethod
182- def from_dict (cls , data : Dict [str , Any ]) -> "DSPyChatGenerator" :
183+ def from_dict (cls , data : dict [str , Any ]) -> "DSPyChatGenerator" :
183184 """Deserialize a component from a dictionary."""
184185 init_params = data .get ("init_parameters" , {})
185186 deserialize_secrets_inplace (init_params , ["api_key" ])
186187 return default_from_dict (cls , data )
187188
188- @component .output_types (replies = List [ChatMessage ])
189+ @component .output_types (replies = list [ChatMessage ])
189190 def run (
190191 self ,
191- messages : List [ChatMessage ],
192- generation_kwargs : Optional [ Dict [ str , Any ]] = None ,
192+ messages : list [ChatMessage ],
193+ generation_kwargs : dict [ str , Any ] | None = None ,
193194 ** kwargs ,
194- ) -> Dict [str , Any ]:
195+ ) -> dict [str , Any ]:
195196 """
196197 Run the DSPy module on the given messages.
197198
@@ -217,13 +218,13 @@ def run(
217218 replies = [ChatMessage .from_assistant (text = output_text )]
218219 return {"replies" : replies }
219220
220- @component .output_types (replies = List [ChatMessage ])
221+ @component .output_types (replies = list [ChatMessage ])
221222 async def run_async (
222223 self ,
223- messages : List [ChatMessage ],
224- generation_kwargs : Optional [ Dict [ str , Any ]] = None ,
224+ messages : list [ChatMessage ],
225+ generation_kwargs : dict [ str , Any ] | None = None ,
225226 ** kwargs ,
226- ) -> Dict [str , Any ]:
227+ ) -> dict [str , Any ]:
227228 """
228229 Asynchronously run the DSPy module on the given messages.
229230
0 commit comments