Skip to content

fix: use .replace() instead of .format() in infer_intent to avoid KeyError on JSON prompt - #19

Merged
pleasingsunlight merged 9 commits into
test-branch-phase4from
intent-based
Apr 9, 2026
Merged

fix: use .replace() instead of .format() in infer_intent to avoid KeyError on JSON prompt#19
pleasingsunlight merged 9 commits into
test-branch-phase4from
intent-based

Conversation

@pleasingsunlight

@pleasingsunlight pleasingsunlight commented Apr 8, 2026

Copy link
Copy Markdown
Collaborator

infer_intent was crashing with KeyError: '"purpose"' because INTENT_PROMPT.format(diff=...) misinterpreted the JSON curly braces in the prompt template as Python format placeholders.

Changes

  • agent/llm/prompts.py — Added missing INTENT_PROMPT with a {diff} placeholder and a JSON output format example
  • agent/llm/groq_client.py — Added infer_intent function using .replace("{diff}", diff[:8000]) instead of .format()
  • agent/llm/test_generator.py — Added intent: dict = None parameter, injected into prompt as ---INTENT--- block
# Before — crashes when INTENT_PROMPT contains {"purpose": ...}
prompt = INTENT_PROMPT.format(diff=diff[:8000])  # KeyError: '"purpose"'

# After — safely substitutes only the {diff} token
prompt = INTENT_PROMPT.replace("{diff}", diff[:8000])

@haddybhaiya

Copy link
Copy Markdown
Member

fix : groq_client.py infer_intent function by using .replace instead of .format

@haddybhaiya haddybhaiya reopened this Apr 9, 2026
@haddybhaiya

Copy link
Copy Markdown
Member

@copilot check for which issue that the check is failing

…_generator with intent param

Agent-Logs-Url: https://github.com/Neuropole/apricot/sessions/f9f90f4a-4747-41aa-98ca-761f97030019

Co-authored-by: haddybhaiya <88364904+haddybhaiya@users.noreply.github.com>

Copilot AI commented Apr 9, 2026

Copy link
Copy Markdown

fix : groq_client.py infer_intent function by using .replace instead of .format

Fixed in commit ecacb68. Added infer_intent to groq_client.py using INTENT_PROMPT.replace("{diff}", diff[:8000]) instead of .format(diff=...). The .format() approach caused KeyError: '"purpose"' because INTENT_PROMPT contains JSON curly braces ({"purpose": ...}) that Python's format parser tries to interpret as placeholders. Using .replace() safely substitutes only the {diff} placeholder without touching the JSON syntax.

@copilot check for which issue that the check is failing

The CI was failing with KeyError: '"purpose"' at groq_client.py line 55 in infer_intent (run 24144564276). Root cause: INTENT_PROMPT.format(diff=diff[:8000]) was used, but INTENT_PROMPT contains raw JSON curly braces ({"purpose": "..."}) that Python's .format() misinterprets as format placeholders. Fixed in commit ecacb68 by:

  • Switching to .replace("{diff}", diff[:8000]) in infer_intent
  • Adding INTENT_PROMPT to prompts.py (it was missing from the branch)
  • Updating test_generator.py to accept the intent parameter

Copilot AI changed the title Enhance intent handling and logging in test generation fix: use .replace() instead of .format() in infer_intent to avoid KeyError on JSON prompt Apr 9, 2026
Copilot AI requested a review from haddybhaiya April 9, 2026 05:22
@haddybhaiya

Copy link
Copy Markdown
Member

apparently the changes proposed are the same changed which are in the branch test-branch-phase3
will see to it

@haddybhaiya haddybhaiya closed this Apr 9, 2026
@haddybhaiya haddybhaiya reopened this Apr 9, 2026
@github-actions

github-actions Bot commented Apr 9, 2026

Copy link
Copy Markdown

Automated Code Review Comment:

Bugs

  • None

Improvements

  • The generate_tests function is improved by adding a docstring to explain its purpose and parameters.
  • Error handling is added when committing tests to handle potential exceptions.
  • The code is refactored for better readability and structure.

Suggestions

  • Consider adding validation for the diff, context, and intent parameters in the generate_tests function.
  • The infer_intent function could be improved by adding more robust error handling and intent extraction logic.
  • Code comments could be added to explain complex logic and improve code readability.
  • The final_output variable could be formatted using a templating engine or f-strings for better readability.
  • Consider adding type hints for the infer_intent function and its return type.

Code looks good overall, but could benefit from additional improvements for better maintainability and robustness. ✅

---

### Suggested Tests
```bash
pytest tests/test_generated.py
def test_empty_diff():
assert generate_tests(diff="") == ""

def test_empty_context():
assert generate_tests(diff="diff --git a/app.py b/app.py", context=[]) == ""

def test_intent_extraction_failure():
assert generate_tests(diff="diff --git a/app.py b/app.py", intent={"error": "intent extraction failed"}) == ""

def test_commit_tests_failure():
try:
commit_tests()
assert False
except Exception as e:
assert str(e) != ""

def test_generate_review_with_intent():
diff = "diff --git a/app.py b/app.py"
context = ["def safe_divide(a, b):"]
intent = {"purpose": "generate tests", "properties": ["a", "b"], "edge_cases": ["a=0", "b=0"]}
assert generate_tests(diff, context, intent) != ""
```

@pleasingsunlight

Copy link
Copy Markdown
Collaborator Author

Good work fixing the files @haddybhaiya , i shall merge the pr once the bot works as intended

@haddybhaiya

Copy link
Copy Markdown
Member

we Ball!!

@pleasingsunlight
pleasingsunlight merged commit aedb19a into test-branch-phase4 Apr 9, 2026
@pleasingsunlight
pleasingsunlight deleted the intent-based branch April 9, 2026 11:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants