Skip to content

Commit dd1e517

Browse files
committed
Add section about using if guards in documentTemplate guide
1 parent 7d6ed21 commit dd1e517

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

learn/ai_powered_search/document_template_best_practices.mdx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ In the sample document, the `overview` alone is 49 words. Use Liquid's [`truncat
5656

5757
Short prompts do not have enough information for the embedder to properly understand the query context. Long prompts instead provide too much information and make it hard for the embedder to identify what is truly relevant about a document.
5858

59+
## Add guards for missing fields
60+
61+
When building your `documentTemplate`, some documents might not contain all the fields you expect. For example, a movie record could be missing the `title` or `overview`. If your template directly references a missing field, it may raise an error.
62+
63+
To prevent this, use Liquid’s `if` statements to add guards around fields. This ensures the template only includes data when it exists:
64+
65+
```
66+
{% if doc.title %}
67+
A movie called {{ doc.title }}
68+
{% endif %}
69+
```
70+
71+
This way, the prompt adapts to each document. If a field is missing, the embedder still receives a valid and useful prompt without errors.
72+
5973
## Conclusion
6074

6175
In this article you saw the main steps to generating prompts that lead to relevant AI-powered search results:

0 commit comments

Comments
 (0)