Querying Structured Data with the inmydata Agent API

Modified on Tue, 24 Jun at 12:56 PM

This article walks you through querying structured data from inmydata using the StructuredDataDriver. Structured data queries are extremely fast, making them ideal for building tools that power conversational interfaces—especially when you need to quickly retrieve data from the inmydata platform in real time. 


You'll learn how to:

  • Connect using your tenant

  • Use a simple filter for basic equality queries

  • Use a complex filter with logical operators and brackets for more advanced conditions

Before starting, make sure you have:

  • Set up your environment variables (INMYDATA_API_KEY, INMYDATA_TENANT, INMYDATA_CALENDAR)

  • Installed the inmydata Python package and python-dotenv via pip


bash

pip install inmydata python-dotenv

1. Load Environment Variables

Start by loading your credentials from the .env file:


python

import os
from dotenv import load_dotenv load_dotenv()

2. Initialize the Structured Data Driver

Create an instance of the StructuredDataDriver using your tenant:


python

from inmydata.StructuredData import StructuredDataDriver
driver = StructuredDataDriver(os.environ['INMYDATA_TENANT'])

3. Use get_data_simple() for Basic Filtering

Use get_data_simple() when your filters are simple equality filters with no brackets or OR logic.

Example: Filter sales data for the Edinburgh store

python

import os
from dotenv import load_dotenv
from inmydata.StructuredData import StructuredDataDriver, AIDataSimpleFilter

load_dotenv()

driver = StructuredDataDriver(os.environ['INMYDATA_TENANT'])

# -- Use get_data_simple when your filter is simple (only equality filters, no bracketing, no ORs, etc.)
# Build our simple filter
filter = []
filter.append(
    AIDataSimpleFilter(
        "Store", # Field to filter on
        "Edinburgh") # Value to filter by
    ) 
df = driver.get_data_simple(
    "Inmystore Sales", # Name of the subject we want to extract data from
    ["Financial Year","Sales Value"], # List of fields we want to extract
    filter, # Filters to apply
    False) # Whether filters are case sensitive

print(df)



4. Use get_data() for Complex Filtering

Use get_data() when your filter requires:

  • Multiple conditions

  • Logical operators like AND/OR

  • Bracketing for precedence

  • Non-equality comparisons

In this example, we build a filter to match records where the Store is either Edinburgh or London. This uses the AIDataFilter class and logical operators to combine conditions.


import os
from dotenv import load_dotenv
from inmydata.StructuredData import StructuredDataDriver, AIDataFilter, LogicalOperator, ConditionOperator

load_dotenv()

driver = StructuredDataDriver(os.environ['INMYDATA_TENANT'])

# -- Use get_data when your filter more complex (non-equality matches, bracketing, ORs, etc.) --

# Build our filter
filter = [] 
filter.append(
    AIDataFilter(
        "Store",
        ConditionOperator.Equals, # Condition to use in the filter
        LogicalOperator.And, # Logical operator to use in the filter
        "Edinburgh", # Value to filter by
        0, # Number of brackets before this condition
        0, # Number of brackets after this condition
        False # Whether the filter is case sensitiv
    )
)
filter.append(
    AIDataFilter(
        "Store",
        ConditionOperator.Equals, # Condition to use in the filter
        LogicalOperator.Or, # Logical operator to use in the filter
        "London", # Value to filter by
        0, # Number of brackets before this condition
        0, # Number of brackets after this condition
        False # Whether the filter is case sensitiv
    )
)
df = driver.get_data(
    "Inmystore Sales", # Name of the subject we want to extract data from
    ["Financial Year","Store","Sales Value"], # List of fields we want to extract
    filter) # Filters to apply

print(df)

? This example only builds the filter and prints the df object. Make sure df has been defined via a call to get_data() beforehand.



Notes

  • Both get_data_simple() and get_data() return a Pandas DataFrame.

  • Use get_data_simple() when possible — it's more readable and easier to construct.

  • Brackets and logical operators in AIDataFilter allow for flexible nesting of conditions.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article