DBT macro receives variable as string when dictionary is passed

Hello Everyone , I’m new to DBT world and facing a strange issue.
Database - Snowflake.
Attached two test models , one test csv data file and one test macro file.
I’m reading data from the seed csv file using call statement and creating a dictionary using fromjson(query_result).
if you look at TEST1 model , this reading data as dictionary is coded there . This dictionary is then passed to the macro scoring and the macro receives it as a dictionary as it should.
Now take a look at TEST2 model. It’s the same thing but the only difference is , the reading data as a dictionary format is done through a macro called get_scoring_metrics .
Here TEST2 receives the dictionary from get_scoring_metrics and pass it on to the macro scoring. But this time the macro scoring receives data as a string instead of a dictionary and throws the exception str object has no attribute items. If you compile the models , you will see it.
How is this possible? Both cases I’m using the same code.
I need data in dictionary format for ease of developing a complicated model .
Any solution will be appreciated . Thank you.

seed data file : sample_data.csv

subdimension,datasource,datasource_label,scoring_metric,weight
facebook_impressions,pathmatics_facebook,Pathmatics Facebook,total_year_impressions,5
facebook_engagement,facebook,Facebook,total_year_interactions,2.5
facebook_engagement,facebook,Facebook,interactions_per_post,2.5


MODEL TEST1 (WORKS CORRECTLY)

– depends_on: {{ ref(‘sample_data’) }}

{%- set datasource = ‘facebook’ -%}

-- DICTIONARY THAT MAPS METRICS TO THEIR SUB-DIMENSIONS

{%- call statement(‘scoring_metric_query’, fetch_result=True) -%}

SELECT TO_JSON(PARSE_JSON('{'||LISTAGG(diq_subdim,',')||'}')) AS diq_metric
FROM
    (
        SELECT subdim_name||':'||'['||scoring_metric||']' AS diq_subdim
        FROM
            (
                SELECT '"'||subdimension ||'"' AS subdim_name,
                LISTAGG(''''||scoring_metric ||'''' , ',') AS scoring_metric
                FROM {{ ref('sample_data') }}
                WHERE lower(datasource) = lower('{{ datasource }}')
                GROUP BY subdimension
            ) T1
    ) T2

{%- endcall -%}

{% if execute %}

{%- set query_result = load_result(‘scoring_metric_query’)[‘data’][0][0] -%}
{% set metric_dict = fromjson(query_result) %}

{% endif %}

{{
scoring(
datasource = datasource,
metric_dict = metric_dict
)
}}


MODEL TEST2 (Which throws exception during compilation even though I’m using same code as model TEST1)

{%- set datasource = ‘facebook’ -%}

{%- set metric_dict = get_scoring_metrics(datasource) -%}

{{
scoring(
datasource = datasource,
metric_dict = metric_dict
)
}}


macro scoring

{% macro scoring(datasource, metric_dict={} ) %}
–the query below means nothing. It’s just an example to show that when this macro is called from test2.sql model, the metric_dict does not
–work as a dictionary but this macro receives metric_dict as a string (error- str objet has no attribute items).
–But when it’s called from test1.sql model it receives metric_dict as a python dictionary.

{% for i , j in metric_dict.items() %}
select {{ i }} , {{ j }}
{% endfor %}

{% endmacro %}


macro get_scoring_metrics

{% macro get_scoring_metrics(datasource) %}

-- DICTIONARY THAT MAPS METRICS TO THEIR SUB-DIMENSIONS

{%- call statement(‘scoring_metric_query’, fetch_result=True) -%}

SELECT TO_JSON(PARSE_JSON('{'||LISTAGG(diq_subdim,',')||'}')) AS diq_metric
FROM
    (
        SELECT subdim_name||':'||'['||scoring_metric||']' AS diq_subdim
        FROM
            (
                SELECT '"'||subdimension ||'"' AS subdim_name,
                LISTAGG(''''||scoring_metric ||'''' , ',') AS scoring_metric
                FROM {{ ref('sample_data') }}
                WHERE lower(datasource) = lower('{{ datasource }}')
                GROUP BY subdimension
            ) T1
    ) T2

{%- endcall -%}

{% if execute %}

{%- set query_result = load_result(‘scoring_metric_query’)[‘data’][0][0] -%}
{% set metric_dict = fromjson(query_result) %}

{{ return(metric_dict) }}

{% endif %}

{% endmacro %}