Prompt Engineering Guide
Mastering Debug code
on Mistral Large 2
Stop guessing. See how professional prompt engineering transforms Mistral Large 2's output for specific technical tasks.
The "Vibe" Prompt
"Debug this code:
```python
import pandas as pd
def process_data(file_path):
df = pd.read_csv(file_path)
df['new_col'] = df['col1'] + df['col2']
return df
# Example usage
# result = process_data('data.csv')
# print(result.head())
# PROBLEM: This code sometimes fails with a TypeError when 'col1' or 'col2' contain non-numeric data.
```"
Low specificity, inconsistent output
Optimized Version
```json
{
"task": "Code Debugging",
"model_target": "Mistral Large 2",
"problem_description": "The provided Python function `process_data` uses pandas to read a CSV, then attempts to sum `col1` and `col2` into a new column. It consistently fails with a `TypeError` when either `col1` or `col2` contain non-numeric data, such as strings or NaNs that prevent arithmetic operations.",
"code_to_debug": """import pandas as pd
def process_data(file_path):
df = pd.read_csv(file_path)
df['new_col'] = df['col1'] + df['col2']
return df""",
"chain_of_thought_steps": [
"1. **Understand the Error Type:** The `TypeError` indicates an operation on incompatible data types. In pandas, adding columns typically requires numeric types.",
"2. **Identify Problematic Operation:** The line `df['new_col'] = df['col1'] + df['col2']` is the direct cause of the error. Addition operation is failing.",
"3. **Hypothesize Root Cause:** The problem description explicitly states 'non-numeric data' in `col1` or `col2`. This would lead to `TypeError` during addition.",
"4. **Brainstorm Solutions for Type Handling:**
a. **Coercion to Numeric:** Convert columns to numeric, handling errors by setting them to `NaN`.
b. **Error Handling/Skipping:** Catch the error or skip non-numeric rows (less ideal for data integrity).
c. **Validation:** Check types before operation and raise a more descriptive error.",
"5. **Select Best Solution:** Coercion (`pd.to_numeric`) is often the most robust solution for this scenario, allowing the operation to proceed while gracefully handling non-numeric entries by making them `NaN`, which can then be further handled (e.g., filled or dropped).
* Consider `errors='coerce'` for `pd.to_numeric` to turn invalid parsing into `NaN`.",
"6. **Formulate Corrected Code:** Apply the chosen solution to the code."
],
"requested_output": "Provide the corrected Python code, an explanation of the fix, and a brief example demonstrating its robustness against mixed data types."
}
```
Structured, task-focused, reduced hallucinations
Engineering Rationale
The optimized prompt leverages a structured JSON format and a detailed chain-of-thought process. It explicitly defines the task, problem, and the exact code, avoiding ambiguity. The chain-of-thought guides the model through understanding the error, identifying the cause, brainstorming solutions, and selecting the most appropriate fix, leading to a more accurate and robust solution. This structured approach mimics human expert debugging, fostering a deeper reasoning process in the LLM.
0%
Token Efficiency Gain
The model should correctly identify `pd.to_numeric(..., errors='coerce')` as the primary solution.
The corrected code should include the type conversion before the addition operation.
The explanation should clearly articulate *why* the original code failed and *how* the fix addresses it.
Ready to stop burning tokens?
Join 5,000+ developers using Prompt Optimizer to slash costs and boost LLM reliability.
Optimize My Prompts