Repository: confident-ai/deepeval
Issue: RuntimeError: asyncio.run() cannot be called from a running event loop when using Bedrock Model in the synthesizer
Root cause: Interface Contract Mismatch



**Describe the bug**
Error when trying to generate synthetic data using Bedrock models.
In [13]: synthesizer.generate_goldens_from_scratch(num_goldens=20)
✨ Generating up to 20 goldens (method=Scratch, evolutions=1) ━━━━━━━━━━━━━━━━━━━━━━   0% 0:00:00
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[13], line 1
----> 1 synthesizer.generate_goldens_from_scratch(num_goldens=20)

#####TRUNCATED####

File ~/.pyenv/versions/3.12.5/envs/global/lib/python3.12/site-packages/deepeval/models/llms/amazon_bedrock_model.py:78, in AmazonBedrockModel.generate(self, prompt, schema)
     75 def generate(
     76     self, prompt: str, schema: Optional[BaseModel] = None
     77 ) -> Tuple[Union[str, Dict], float]:
---> 78     return asyncio.run(self.a_generate(prompt, schema))

File ~/.pyenv/versions/3.12.5/lib/python3.12/asyncio/runners.py:190, in run(main, debug, loop_factory)
    161 """Execute the coroutine and return the result.
    162
    163 This function runs the passed coroutine, taking care of
   (...)
    186     asyncio.run(main())
    187 """
    188 if events._get_running_loop() is not None:
    189     # fail fast with short traceback
--> 190     raise RuntimeError(
    191         "asyncio.run() cannot be called from a running event loop")
    193 with Runner(debug=debug, loop_factory=loop_factory) as runner:
    194     return runner.run(main)

RuntimeError: asyncio.run() cannot be called from a running event loop

In [14]: synthesizer.generate_goldens_from_scratch(num_goldens=20)
✨ Generating up to 20 goldens (method=Scratch, evolutions=1) ━━━━━━━━━━━━━━━━━━━━━━   0% 0:00:00
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[14], line 1
----> 1 synthesizer.generate_goldens_from_scratch(num_goldens=20)

File ~/.pyenv/versions/3.12.5/envs/global/lib/python3.12/site-packages/deepeval/synthesizer/synthesizer.py:872, in Synthesizer.generate_goldens_from_scratch(self, num_goldens, _send_data)
    869 if self.async_mode:
    870     loop = get_or_create_event_loop()
    871     goldens.extend(
--> 872         loop.run_until_complete(
    873             self.a_generate_goldens_from_scratch(
    874                 num_goldens=num_goldens,
    875             )
    876         )
    877     )
    878 else:
    879     with synthesizer_progress_context(
    880         method="Scratch",
    881         num_evolutions=self.evolution_config.num_evolutions,
   (...)
    889
    890         # Generate inputs

File ~/.pyenv/versions/3.12.5/lib/python3.12/asyncio/base_events.py:687, in BaseEventLoop.run_until_complete(self, future)
    684 if not future.done():
    685     raise RuntimeError('Event loop stopped before Future completed.')
--> 687 return future.result()

File ~/.pyenv/versions/3.12.5/envs/global/lib/python3.12/site-packages/deepeval/synthesizer/synthesizer.py:810, in Synthesizer.a_generate_goldens_from_scratch(self, num_goldens)
    793 with synthesizer_progress_context(
    794     method="Scratch",
    795     num_evolutions=self.evolution_config.num_evolutions,
   (...)
    802 ) as (progress, pbar_id), progress:
    803     # Generate inputs
    804     prompt = PromptSynthesizerTemplate.generate_synthetic_prompts(
    805         scenario=self.styling_config.scenario,
    806         task=self.styling_config.task,
    807         input_format=self.styling_config.input_format,
    808         num_goldens=num_goldens,
    809     )
--> 810     synthetic_data = self._generate_inputs(prompt)
    811     update_pbar(progress, pbar_id)
    813     # Evolve inputs

File ~/.pyenv/versions/3.12.5/envs/global/lib/python3.12/site-packages/deepeval/synthesizer/synthesizer.py:1072, in Synthesizer._generate_inputs(self, prompt)
   1071 def _generate_inputs(self, prompt: str) -> List[SyntheticData]:
-> 1072     res: SyntheticDataList = self._generate_schema(
   1073         prompt, SyntheticDataList, self.model
   1074     )
   1075     synthetic_data_items = res.data
   1076     return synthetic_data_items

File ~/.pyenv/versions/3.12.5/envs/global/lib/python3.12/site-packages/deepeval/synthesizer/synthesizer.py:1249, in Synthesizer._generate_schema(self, prompt, schema, model)
   1242 def _generate_schema(
   1243     self,
   1244     prompt: str,
   1245     schema: BaseModel,
   1246     model: DeepEvalBaseLLM,
   1247 ) -> BaseModel:
   1248     if is_native_model(model):
-> 1249         res, cost = model.generate(prompt, schema)
   1250         if self.synthesis_cost is not None:
   1251             self.synthesis_cost += cost

File ~/.pyenv/versions/3.12.5/envs/global/lib/python3.12/site-packages/deepeval/models/llms/amazon_bedrock_model.py:78, in AmazonBedrockModel.generate(self, prompt, schema)
     75 def generate(
     76     self, prompt: str, schema: Optional[BaseModel] = None
     77 ) -> Tuple[Union[str, Dict], float]:
---> 78     return asyncio.run(self.a_generate(prompt, schema))

File ~/.pyenv/versions/3.12.5/lib/python3.12/asyncio/runners.py:190, in run(main, debug, loop_factory)
    161 """Execute the coroutine and return the result.
    162
    163 This function runs the passed coroutine, taking care of
   (...)
    186     asyncio.run(main())
    187 """
    188 if events._get_running_loop() is not None:
    189     # fail fast with short traceback
--> 190     raise RuntimeError(
    191         "asyncio.run() cannot be called from a running event loop")
    193 with Runner(debug=debug, loop_factory=loop_factory) as runner:
    194     return runner.run(main)

RuntimeError: asyncio.run() cannot be called from a running event loop


**To Reproduce**
Steps to reproduce the behavior:
from deepeval.synthesizer import Synthesizer
from deepeval.synthesizer.config import StylingConfig

styling_config = StylingConfig(
  input_format="Questions in English that asks for data in database.",
  expected_output_format="SQL query based on the given input",
  task="Answering text-to-SQL-related queries by querying a database and returning the results to users",
  scenario="Non-technical users trying to query a database using plain English.",
)

model = AmazonBedrockModel(
       model_id="us.anthropic.claude-3-opus-20240229-v1:0",
        temperature=0,
        region_name="us-west-2"


synthesizer = Synthesizer(styling_config=styling_config)
**Expected behavior**





We had similar issues, try configuring nest_asyncio before importing any of your code:
```
import nest_asyncio
nest_asyncio.apply()
```

Hey @tech4life87, we fixed this recently, let us know if you face this issue again, for now, I'm gonna close this PR 🙂.