Configuration
Path to the file to edit. Can be absolute or relative to the working directory (
config/extended_openai_conversation/).Exact text to find and replace. Must appear exactly once in the file.
Text to replace the old text with.
Optional list of additional allowed directories (as templates). By default, only the working directory is allowed.
Working Directory
The default working directory isconfig/extended_openai_conversation/.
- Relative Path
- Subdirectory
- Absolute Path
Examples
Edit File
Custom Allowed Directories
Extend allowed directories beyond the default workspace. Theallow_dir parameter adds additional directories where files can be edited.
How It Works
Edit File is safer than Write File because it:- Reads the current file to verify it exists
- Searches for exact match of
old_text - Validates single occurrence - fails if text appears 0 or 2+ times
- Performs replacement - replaces only the one matched occurrence
- Writes back to the same file
Occurrence check
- If found 0 times → Error: “Text not found in file”
- If found 1 time → Proceed with replacement
- If found 2+ times → Error: “Text appears N times in file”
Return Value
On success:Safety Features
Exact Match Required
Exact Match Required
old_text must match exactly, including:- Whitespace (spaces, tabs, newlines)
- Case sensitivity
- Special characters
Single Occurrence Validation
Single Occurrence Validation
If text appears multiple times, the edit is rejected. This prevents unintended changes to multiple locations.To edit when text appears multiple times, make
old_text more specific by including surrounding context.File Must Exist
File Must Exist
Unlike Write File, Edit File requires the file to already exist. This prevents accidental file creation.
Atomic Operation
Atomic Operation
The entire read-modify-write operation is atomic. If any step fails, the file remains unchanged.
Common Patterns
Update YAML Configuration Value
Update Python Variable
Comment Out a Line
Update Automation Trigger
Use Cases
Configuration Updates
Update settings in YAML, JSON, or INI files
Version Updates
Change version numbers or identifiers
Entity ID Changes
Update entity references in automations
Text Corrections
Fix typos or update documentation
Advanced Examples
Multi-line Replacement
Edit File supports multi-line text:Replace with Empty String (Delete)
To delete a line, include the newline character (
\n) in old_text so the empty line is also removed.Conditional Edit with Validation
Best Practices
Include context for uniqueness
When the value appears multiple times, include surrounding text to make it unique:
Preserve exact formatting
Match indentation, spaces, and newlines exactly as they appear in the file:
Read file first to verify content
Use composite function to read file first and verify the content exists:
Troubleshooting
Text not found in file
Text not found in file
The
old_text doesn’t exist in the file. Common causes:- Typo in
old_text - Whitespace mismatch (spaces vs tabs)
- Case sensitivity
- File has already been edited
Text appears N times in file
Text appears N times in file
The
old_text appears multiple times. This is blocked to prevent unintended changes.Solution: Make old_text more specific by including surrounding context:File not found
File not found
The file doesn’t exist at the specified path. Edit File requires existing files.Solution: Use Write File to create new files, or verify the file path is correct.
Access denied
Access denied
The file is outside allowed directories.Solution: Add the directory to
allow_dir or move the file to the workspace.Comparison: Edit vs Write
When to use Edit File
When to use Edit File
✅ Use Edit File when:
- Modifying existing files
- You want to change specific values
- You need safety against accidental overwrites
- The file has other content you want to preserve
When to use Write File
When to use Write File
✅ Use Write File when:
- Creating new files from scratch
- Generating complete file contents
- You want to overwrite the entire file
- The file is a generated output (logs, reports, exports)
FAQ
Can I replace multiple occurrences at once?
Can I replace multiple occurrences at once?
No. Edit File only replaces a single occurrence. This is a safety feature. To replace multiple occurrences, either:
- Make
old_textmore specific so it matches only once - Use bash functions with
sed:
What if I need to edit the same value multiple times?
What if I need to edit the same value multiple times?
Call the edit function multiple times, each time making
old_text specific to one occurrence by including surrounding context.Can I use regular expressions?
Can I use regular expressions?
No. Edit File uses exact string matching. For regex replacements, use bash functions with
sed.Does it create backups?
Does it create backups?
No. The file is modified in place. If you need backups, use a composite function:
Security
Path Restriction
Path Restriction
Files must be within allowed directories. By default:
/config/extended_openai_conversation/(working directory)
allow_dir.Exact Match Only
Exact Match Only
Only exact string matches are replaced. This prevents unintended changes through fuzzy matching or partial matches.
Single Occurrence
Single Occurrence
Only one occurrence can be replaced at a time. This prevents mass replacements that could corrupt the file.
File Must Exist
File Must Exist
The file must already exist. This prevents accidental file creation.