Skip to content

Commit c8253f1

Browse files
Update agentic-rag.mdx: rewrite_question Node: Role and Content based… (#1334)
… dictionary replaced with LangChain Human Message As per the docs live officially, in the rewrite_question node, the response of the model for rewriting the query is returned in dictionary format to messages in the state. return {"messages": [{"role": "user", "content": response.content}]} I understand this has been done keeping in mind to modify the message's role from ai/assistant to user in the state. While this is not critical for the time being, it might cause errors while printing graph's streaming updates. for node, update in chunk.items(): print("Update from node", node) update["messages"][-1].pretty_print() print("\n\n") This might have been skipped due to running an example where rewrite node was not required. However, if the rewrite node comes into play, it would push the dictionary-based format of messages and not the standard Messages class format. The pretty print function is valid only on Message classes and not a dictionary; this would lead to an attribute error: AttributeError: 'dict' object has no attribute 'pretty_print' For AI learners, especially those who have been exploring LangChain v1.0 docs might get blocked due to this while following the tutorial. Please merge this one so that it adds to the quality and trust for LangChain docs. ## Overview <!-- Brief description of what documentation is being added/updated --> ## Type of change **Type:** [Replace with: New documentation page / Update existing documentation / Fix typo/bug/link/formatting / Remove outdated content / Other] ## Related issues/PRs <!-- Link to related issues, feature PRs, or discussions (if applicable) To automatically close an issue when this PR is merged, use closing keywords: - "closes #123" or "fixes #123" or "resolves #123" For regular references without auto-closing, just use: - "#123" or "See issue #123" Examples: - closes #456 (will auto-close issue #456 when PR is merged) - See #789 for context (will reference but not auto-close issue #789) --> - GitHub issue: - Feature PR: <!-- For LangChain employees, if applicable: --> - Linear issue: - Slack thread: ## Checklist <!-- Put an 'x' in all boxes that apply --> - [ ] I have read the [contributing guidelines](README.md) - [ ] I have tested my changes locally using `docs dev` - [x] All code examples have been tested and work correctly - [x] I have used **root relative** paths for internal links - [x] I have updated navigation in `src/docs.json` if needed - I have gotten approval from the relevant reviewers - (Internal team members only / optional) I have created a preview deployment using the [Create Preview Branch workflow](https://github.com/langchain-ai/docs/actions/workflows/create-preview-branch.yml) ## Additional notes <!-- Any other information that would be helpful for reviewers -->
1 parent c901059 commit c8253f1

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/oss/langgraph/agentic-rag.mdx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,8 @@ Note that the components will operate on the [`MessagesState`](/oss/langgraph/gr
523523
:::python
524524
1. Build the `rewrite_question` node. The retriever tool can return potentially irrelevant documents, which indicates a need to improve the original user question. To do so, we will call the `rewrite_question` node:
525525
```python
526+
from langchain.messages import HumanMessage
527+
526528
REWRITE_PROMPT = (
527529
"Look at the input and try to reason about the underlying semantic intent / meaning.\n"
528530
"Here is the initial question:"
@@ -539,7 +541,7 @@ Note that the components will operate on the [`MessagesState`](/oss/langgraph/gr
539541
question = messages[0].content
540542
prompt = REWRITE_PROMPT.format(question=question)
541543
response = response_model.invoke([{"role": "user", "content": prompt}])
542-
return {"messages": [{"role": "user", "content": response.content}]}
544+
return {"messages": [HumanMessage(content=response.content)]}
543545
```
544546
2. Try it out:
545547
```python
@@ -567,7 +569,7 @@ Note that the components will operate on the [`MessagesState`](/oss/langgraph/gr
567569
}
568570

569571
response = rewrite_question(input)
570-
print(response["messages"][-1]["content"])
572+
print(response["messages"][-1].content)
571573
```
572574
**Output:**
573575
```

0 commit comments

Comments
 (0)