ValueError: Node must be a TextNode to get text.
text
attribute of a node that is not a TextNode
. According to the LlamaIndex API reference, you're likely dealing with a NodeWithScore
object instead of a TextNode
.TextNode
within the NodeWithScore
object. Here's how you can modify your code:node.text
, use node.node.text
:nodes_text += node.node.text
if isinstance(node, TextNode): nodes_text += node.text elif isinstance(node, NodeWithScore): nodes_text += node.node.text else: # Handle other node types or raise an error
NodeWithScore
class is a wrapper that contains both the node and its relevance score. To access the actual text content, you need to go through the node
attribute of NodeWithScore
.get_text()
method, which is available on both TextNode
and NodeWithScore
objects:nodes_text += node.get_text()
TextNode
and NodeWithScore
objects, as shown in the LlamaIndex API reference.ValueError
and successfully access the text content of your nodes.import llama_index # Assuming 'node' is your variable containing the node object node_type = type(node) print(f"The type of the node is: {node_type}") # You can also check if it's an instance of specific classes if isinstance(node, llama_index.core.schema.NodeWithScore): print("This is a NodeWithScore object") print(f"The type of the inner node is: {type(node.node)}") elif isinstance(node, llama_index.core.schema.TextNode): print("This is a TextNode object") else: print(f"This is neither a NodeWithScore nor a TextNode, but a {node_type}") # To see all attributes of the node print("Attributes of the node:") for attr in dir(node): if not attr.startswith("__"): # Skip internal attributes print(f"{attr}: {getattr(node, attr)}")
TextNode
, ImageNode
, and the generic Node
class. The NodeWithScore
is a wrapper that can contain any of these node types.NodeWithScore
, you can access the underlying node and its text like this:if isinstance(node, llama_index.core.schema.NodeWithScore): print(f"Score: {node.score}") print(f"Underlying node type: {type(node.node)}") print(f"Text content: {node.node.get_content()}")
get_content()
instead of directly accessing text
, as it's a more reliable method that works across different node types.NodeWithScore
, but its inner node is a Document
, not a TextNode
. This explains why you're still getting the ValueError
when trying to use get_text()
.Document
. Here's how you can modify your code to handle this situation:from llama_index.core.schema import NodeWithScore, TextNode, Document # Assuming 'node' is your variable containing the node object if isinstance(node, NodeWithScore): print(f"Score: {node.score}") inner_node = node.node print(f"Underlying node type: {type(inner_node)}") if isinstance(inner_node, TextNode): nodes_text += inner_node.get_text() elif isinstance(inner_node, Document): nodes_text += inner_node.get_content() else: print(f"Unexpected inner node type: {type(inner_node)}") elif isinstance(node, TextNode): nodes_text += node.get_text() else: print(f"Unexpected node type: {type(node)}")
NodeWithScore
is a Document
. The Document
class has a get_content()
method that you can use to retrieve the text content.NodeWithScore
class can wrap different types of nodes, including Document
objects. The LlamaIndex API reference shows that NodeWithScore
can contain various node types.Document
objects inside NodeWithScore
, you might want to adjust your code to always use get_content()
instead of get_text()
:if isinstance(node, NodeWithScore): nodes_text += node.node.get_content() else: nodes_text += node.get_content()
TextNode
and Document
objects, whether they're wrapped in a NodeWithScore
or not.llama_index.core.schema
at the beginning of your script. By implementing these changes, you should be able to handle the different node types correctly and avoid the ValueError
.