query
method which retrieves and generates responses from existing documents, the agent can dynamically decide which tools to use based on the query requirements, execute code, analyze documents, create graphs, and more.
Parameters
query
(str): Natural language query for the Morphik agent
Returns
Dict[str, Any]
: A comprehensive response dictionary containing:response
(str): The agent’s final text responsetool_history
(List[Dict]): Detailed history of all tool executionsdisplay_objects
(List[Dict]): Structured content objects for rich displaysources
(List[Dict]): Source attribution for all referenced content
Response Structure
The agent response contains four main components:1. response
(str)
The agent’s final textual response to your query.
2. tool_history
(List[Dict])
A chronological list of all tools the agent executed, where each entry contains:
tool_name
(str): Name of the executed tooltool_args
(dict): Arguments passed to the tooltool_result
(str/dict): The result returned by the tool
3. display_objects
(List[Dict])
Structured content objects for rich display, where each object contains:
type
(str): Either “text” or “image”content
(str): For text objects, this is markdown content. For image objects, this is the image datasource
(str): Source ID linking to the original contentcaption
(str, for images): Description or caption for image objects
4. sources
(List[Dict])
Source attribution information, where each source contains:
sourceId
(str): Unique identifier for the sourcedocumentName
(str): Human-readable document namedocumentId
(str): Document identifiercontent
(str): Relevant content from the source
Available Tools
The agent has access to the following tools and will automatically choose which ones to use:Document Operations
- retrieve_chunks: Retrieve relevant text and image chunks from the knowledge base
- retrieve_document: Get full document content or metadata
- document_analyzer: Analyze documents for entities, facts, summary, sentiment, or full analysis
- list_documents: List documents accessible to the agent
Knowledge Graph Operations
- knowledge_graph_query: Query knowledge graphs for entities, paths, subgraphs, or list entities
- list_graphs: List available knowledge graphs
Computation & Analysis
- execute_code: Run Python code in a safe sandbox environment
Memory Management
- save_to_memory: Save important information to persistent memory for future queries
Examples
Simple Query
Complex Analysis Request
Knowledge Graph Exploration
Working with Display Objects
Key Differences from query
Method
Feature | query | agent_query |
---|---|---|
Approach | Retrieval-based: finds relevant chunks and generates response | Agent-based: autonomously decides and uses tools |
Tools | None (fixed retrieval pipeline) | Full tool access (retrieve, analyze, code, graphs, etc.) |
Response Type | CompletionResponse object | Dict[str, Any] with rich structure |
Autonomy | Fixed workflow | Dynamic decision making |
Capabilities | Document retrieval and QA | Multi-modal analysis, computation, memory |
Use Case | Direct questions about documents | Complex research and analysis tasks |
Best Practices
1. Query Formulation
- Be specific about what you want the agent to accomplish
- Include context about the type of analysis or information needed
- Mention if you want calculations, comparisons, or visualizations
2. Response Processing
- Always check the
tool_history
to understand what the agent did - Use
display_objects
for rich content presentation - Reference
sources
for attribution and verification
3. Error Handling
4. Memory Usage
The agent can save important information to memory for future queries:Limitations
- Cloud Mode: Free tier accounts have limits on agent calls
- Context Window: Very complex queries might exceed the model’s context window
- Tool Availability: Some tools may not be available depending on your configuration
- Processing Time: Agent queries typically take longer than regular queries due to tool execution
Related Methods
query
: For simple document-based Q&Aretrieve_chunks
: For manual document retrievalcreate_graph
: For knowledge graph creationlist_documents
: For document discovery