# Slicing logic to remove ")

> Discover how llm_utils.py in hiring-agent cleans up response artifacts by slicing logic to remove internal reasoning. Improve your data accuracy.

- Repository: [HackerRank/hiring-agent](https://github.com/interviewstreet/hiring-agent)
- Tags: how-to-guide
- Published: 2026-07-20

---

"))
- Removes internal reasoning artifacts

H3: Markdown Code Fence Extraction
- Detects ```json blocks
- Extracts inner content, handles missing closing fences

H3: Escaped Quote Sequences
- Converts \" to "
- Handles double-escaped characters

Actually, looking at the raw analysis again, the table mentions:
- Leading/trailing whitespace
- Embedded ``, then `think_end + 8` would skip `` (7 chars) plus one more? Or maybe `think_end` points to the `>`?
- Actually `` is 8 characters: < (1) / (2) t (3) h (4) i (5) n (6) k (7) > (8)
- So if `think_end` is the index of `<`, then `think_end + 8` is the index right after `>`.
- So yes, this removes `` blocks.

For the code fences, typically the logic involves finding ```json and the closing ```.

Given the constraints, I will structure the article to cover these specific technical implementations mentioned in the analysis.

Wait, I should check if there's any other edge case mentioned. The table seems to have 3-4 rows but the text is fragmented. I see "Missing ````" which could be "Missing closing backticks" or "Missing code fences".

Let me proceed with:
1. Whitespace normalization
2. Reasoning tag removal (")
    if think_start != -1 and think_end != -1:
        response_text = response_text[:think_start] + response_text[think_end + 8:]
    
    # ... additional logic for code fences and escapes

```

This is a reconstruction based on the analysis provided. The analysis explicitly mentions the slicing logic and the strip() call.

For the FAQ:
- What is the purpose of extract_json_from_response?
- How does it handle model reasoning tags?
- Why strip whitespace before JSON parsing?
- What happens if  # Edge Cases in llm_utils.py Response Cleanup: Whitespace, Tags, and Escapes Explained

**The `extract_json_from_response` function in `llm_utils.py` (lines 13-38) defensively handles four critical LLM output anomalies—leading/trailing whitespace, embedded `` XML tags. These artefacts contaminate the JSON payload if preserved. The function detects these tags using index slicing and reconstructs the string with the reasoning block removed:

```python

# Slicing logic to remove ")

if think_start != -1 and think_end != -1:
    response_text = response_text[:think_start] + response_text[think_end + 8:]

```

The offset `+ 8` accounts for the exact length of the closing `` tag (8 characters), ensuring complete removal without residue.

### Markdown Code Fence Variations

Models often wrap JSON inside markdown code blocks (```json ... ```), and occasionally omit closing fences. The cleanup logic identifies fenced code blocks and extracts only the interior content, handling cases where the outer backticks are present or malformed. This extraction prevents markdown syntax from triggering JSON parse errors while accommodating incomplete fence markers.

### Escaped Character Sequences

When models return JSON embedded within string literals or pre-escaped for markdown contexts, quote characters appear as escaped sequences (`\"`). The function converts these escaped sequences to literal characters where necessary, ensuring the final output is valid JSON:

```python

# Example transformation from the source analysis

raw_input = '{\\"status\\": \\"ok\\"}'
clean = extract_json_from_response(raw_input)

# Result: '{"status": "ok"}'

```

## Implementation Walkthrough

According to the `interviewstreet/hiring-agent` source code, the complete cleanup pipeline executes defensive transformations in sequence:

1. **Strip** whitespace to establish clean boundaries
2. **Extract** content from markdown fences if present  
3. **Remove** `` delimiter completely.

### Why does the cleanup logic strip whitespace before processing JSON?

Leading and trailing whitespace—including newline characters and spaces—are valid incentives for Python's `json.loads()` to raise a `JSONDecodeError` when they appear before the opening `{` or after the closing `}`. The `strip()` operation ensures the parser receives exactly the JSON payload without peripheral formatting noise.

### Does the function handle markdown code fences around the JSON?

Yes. The implementation detects markdown code fences (```json ... ```) and extracts the inner JSON content, accommodating both complete and incomplete fence markers. This prevents markdown syntax from corrupting the extracted data structure.