From b87bf02d96a44eb51d12f140b3b5c8dfe694542b Mon Sep 17 00:00:00 2001 From: Juntaoyou Date: Mon, 15 Dec 2025 15:14:39 +0800 Subject: [PATCH 1/2] docs(Parallel): improve class docstring to follow Google style --- dspy/predict/parallel.py | 58 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/dspy/predict/parallel.py b/dspy/predict/parallel.py index 91f5c1812b..aa66ec4c4a 100644 --- a/dspy/predict/parallel.py +++ b/dspy/predict/parallel.py @@ -16,6 +16,64 @@ def __init__( provide_traceback: bool | None = None, disable_progress_bar: bool = False, ): + """ + A utility class for parallel, multi-threaded execution of (module, example) pairs. + + Supports various example formats (e.g., `Example`, dict, tuple, list), robust error handling, + optional progress tracking, and can optionally return failed examples and exceptions. + + Args: + num_threads (Optional[int]): The number of threads to use. Defaults to `settings.num_threads`. + max_errors (Optional[int]): The maximum number of errors allowed before raising an exception. Defaults to `settings.max_errors`. + access_examples (bool): Whether to unpack `Example` objects via `.inputs()`. Defaults to True. + return_failed_examples (bool): Whether to return failed examples. Defaults to False. + provide_traceback (Optional[bool]): Whether to provide traceback. Defaults to None. + disable_progress_bar (bool): Whether to disable progress bar. Defaults to False. + + Example: + ```python + import dspy + + dspy.configure(lm=dspy.LM("openai/gpt-4o-mini")) + + # define a module to process data + class MyModule(dspy.Module): + def __init__(self): + super().__init__() + + def forward(self, question: str) -> str: + return f"Answer to: {question}" + + # create example list + examples = [ + dspy.Example(question="What is the capital of France?").with_inputs("question"), + dspy.Example(question="What is 2 + 2?").with_inputs("question"), + dspy.Example(question="Who invented electricity?").with_inputs("question"), + dspy.Example(question="What is the largest planet?").with_inputs("question"), + dspy.Example(question="What is the chemical symbol for water?").with_inputs("question"), + ] + + # create module instance + module = MyModule() + + # create execution pairs + exec_pairs = [(module, example) for example in examples] + + # parallel execution + results = parallel(exec_pairs, num_threads=3, disable_progress_bar=False) + + # print results + for i, result in enumerate(results): + print(f"Result {i+1}: {result}") + + # Expected Output: + # Result 1: Answer to: What is the capital of France? + # Result 2: Answer to: What is 2 + 2? + # Result 3: Answer to: Who invented electricity? + # Result 4: Answer to: What is the largest planet? + # Result 5: Answer to: What is the chemical symbol for water? + ``` + """ super().__init__() self.num_threads = num_threads or settings.num_threads self.max_errors = settings.max_errors if max_errors is None else max_errors From d2c2a3d831bf6f0d008fc994b67589718ee759c0 Mon Sep 17 00:00:00 2001 From: Juntaoyou Date: Mon, 22 Dec 2025 14:07:45 +0800 Subject: [PATCH 2/2] [doc]add_docstrings_for_parallel --- dspy/predict/parallel.py | 44 ++++++++++++---------------------------- 1 file changed, 13 insertions(+), 31 deletions(-) diff --git a/dspy/predict/parallel.py b/dspy/predict/parallel.py index aa66ec4c4a..553b5e7abf 100644 --- a/dspy/predict/parallel.py +++ b/dspy/predict/parallel.py @@ -33,45 +33,27 @@ def __init__( Example: ```python import dspy + from dspy.predict.parallel import Parallel + lm = dspy.LM("openai/gpt-4o-mini") + dspy.configure(lm=lm) - dspy.configure(lm=dspy.LM("openai/gpt-4o-mini")) - - # define a module to process data - class MyModule(dspy.Module): - def __init__(self): - super().__init__() - - def forward(self, question: str) -> str: - return f"Answer to: {question}" - - # create example list examples = [ - dspy.Example(question="What is the capital of France?").with_inputs("question"), - dspy.Example(question="What is 2 + 2?").with_inputs("question"), - dspy.Example(question="Who invented electricity?").with_inputs("question"), - dspy.Example(question="What is the largest planet?").with_inputs("question"), - dspy.Example(question="What is the chemical symbol for water?").with_inputs("question"), + {"question": "What is the capital of Spain?"}, + {"question": "What is 3 * 4?"}, + {"question": "Who wrote Hamlet?"}, ] - # create module instance - module = MyModule() - - # create execution pairs + module=dspy.Predict("question->answer") exec_pairs = [(module, example) for example in examples] - - # parallel execution - results = parallel(exec_pairs, num_threads=3, disable_progress_bar=False) - - # print results + parallel = Parallel(num_threads=3, disable_progress_bar=False) + results = parallel(exec_pairs) for i, result in enumerate(results): - print(f"Result {i+1}: {result}") + print(f"Result {i+1}: {result.answer}") # Expected Output: - # Result 1: Answer to: What is the capital of France? - # Result 2: Answer to: What is 2 + 2? - # Result 3: Answer to: Who invented electricity? - # Result 4: Answer to: What is the largest planet? - # Result 5: Answer to: What is the chemical symbol for water? + # Result 1: Madrid + # Result 2: 12 + # Result 3: William Shakespeare ``` """ super().__init__()