Implementation:Explodinggradients Ragas PromptUtils Module
| Field | Value |
|---|---|
| source | Explodinggradients_Ragas (GitHub) |
| domains | Prompts, Utilities |
| last_updated | 2026-02-10 00:00 GMT |
Overview
The prompt.utils module provides utility functions for recursively extracting strings from nested structures, replacing strings in those structures, and extracting JSON from LLM text output.
Description
get_all_strings recursively traverses any combination of strings, Pydantic models, lists, tuples, and dictionaries to collect all string values into a flat list. update_strings performs the inverse operation: given parallel lists of old and new strings, it deep-copies the structure and replaces each exact string match with its corresponding new string. Both functions are essential for the prompt adaptation/translation workflow. extract_json identifies the first JSON structure (object or array) in a text blob by matching balanced delimiters. It handles markdown-wrapped JSON (triple-backtick blocks) by starting search after the ```json marker. If no balanced JSON structure is found, the original text is returned.
Usage
Use get_all_strings and update_strings together for prompt translation workflows. Use extract_json to parse LLM outputs that contain JSON embedded in natural language text.
Code Reference
| Item | Detail |
|---|---|
| Source Location | src/ragas/prompt/utils.py L7-106
|
| Functions | get_all_strings(obj) -> list[str], update_strings(obj, old_strings, new_strings) -> Any, extract_json(text) -> str
|
| Import | from ragas.prompt.utils import get_all_strings, update_strings, extract_json
|
I/O Contract
Inputs
| Function | Parameter | Type | Description |
|---|---|---|---|
get_all_strings |
obj |
Any |
Nested structure (str, BaseModel, list, tuple, dict) |
update_strings |
obj |
Any |
Nested structure to update |
update_strings |
old_strings |
list[str] |
Strings to find (parallel with new_strings) |
update_strings |
new_strings |
list[str] |
Replacement strings (parallel with old_strings) |
extract_json |
text |
str |
Raw LLM output text potentially containing JSON |
Outputs
| Function | Return Type | Description |
|---|---|---|
get_all_strings |
list[str] |
Flat list of all string values found |
update_strings |
Any |
Deep copy of obj with strings replaced |
extract_json |
str |
Extracted JSON string, or original text if no JSON found |
Usage Examples
from ragas.prompt.utils import get_all_strings, update_strings, extract_json
# Extract all strings from nested structures
data = {"a": "hello", "b": ["world", "foo"], "c": {"d": "bar"}}
strings = get_all_strings(data)
print(strings) # ['hello', 'world', 'foo', 'bar']
# Replace strings in nested structures
updated = update_strings(
data,
old_strings=["hello", "world", "foo", "bar"],
new_strings=["hola", "mundo", "baz", "qux"],
)
print(updated) # {'a': 'hola', 'b': ['mundo', 'baz'], 'c': {'d': 'qux'}}
# Extract JSON from LLM output
text = 'Here is the result: {"score": 0.85, "reason": "good"} hope that helps!'
json_str = extract_json(text)
print(json_str) # '{"score": 0.85, "reason": "good"}'
# Handles markdown code blocks
md_text = '```json\n{"answer": "yes"}\n```'
print(extract_json(md_text)) # '{"answer": "yes"}'
Related Pages
- PydanticPrompt_Class - Uses extract_json for output parsing and get_all_strings/update_strings for adapt()
- MetricBasePrompt_Class - Uses get_all_strings/update_strings for adapt()
- BasePrompt_Class - Base prompt classes that rely on these utilities