# Python model injecting SQL

**URL:** https://discourse.getdbt.com/t/python-model-injecting-sql/7490
**Category:** Help
**Tags:** python-models
**Created:** [March 27, 2023, 12:54pm UTC](https://discourse.getdbt.com/t/python-model-injecting-sql/7490 "2023-03-27T12:54:31Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Jan](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@Jan](https://discourse.getdbt.com/u/Jan)
#### Post date: [March 27, 2023, 12:54pm UTC](https://discourse.getdbt.com/t/python-model-injecting-sql/7490/1 "2023-03-27T12:54:31Z")

</div>

Welcome everybody! 🙂

## The problem I’m having

I just started using python models and right on the first one I am experiencing some weird behaviour. My model bases on an SQL model (which I think is valid given the documentation [here](https://docs.getdbt.com/docs/build/python-models#referencing-other-models)). However, when generating the .py file, it injects pure SQL into the code

### Input (model.py)

```import

def model(dbt, session):
    dbt.config(materialized = "table")
    df = dbt.ref("my-sql-model")

    dx = df
    ...

```

### Output (.py file on GCS)

```auto
from pyspark.sql import SparkSession

spark = SparkSession.builder.appName('smallTest').getOrCreate()

spark.conf.set("viewsEnabled","true")
spark.conf.set("temporaryGcsBucket","my-bucket-name")

with __dbt__ cte __dictionary__ xxx as (
select
    something1 as column1,
    something2 as column2
from
    `my-project`.`my-dataset`.`my-table`
where
    xxx
)def model(dbt, session):
    dbt.config(materialized = "table")
    import pandas as pd
    df = dbt.ref("my-sql-model")

    dx = df

```

### Setup

BigQuery, GCS, dbt Cloud v1.3

There is not much I was able to do here as I really don’t understand where it is coming from. Did you ever face that issue before?

---

<div class="post-metadata">

### Author: ![Jan](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@Jan](https://discourse.getdbt.com/u/Jan)
#### Post date: [March 27, 2023, 1:59pm UTC](https://discourse.getdbt.com/t/python-model-injecting-sql/7490/2 "2023-03-27T13:59:37Z")

</div>

I spent some more time with it and I noticed that that the base model being set to ephemeral is causing this issue. Probably, I should’ve caught it much earlier cause the SQL is pretty distinct.

Still, isn’t it somewhat of a bug? For example, ephemeral model could still be passed as a python query or there could be a validation that would prevent from using such models in python models.

---

<div class="post-metadata">

### Author: ![joellabes](https://sea2.discourse-cdn.com/flex020/user_avatar/discourse.getdbt.com/joellabes/32/3980_2.png) [@joellabes](https://discourse.getdbt.com/u/joellabes)
#### Post date: [March 28, 2023, 5:46am UTC](https://discourse.getdbt.com/t/python-model-injecting-sql/7490/3 "2023-03-28T05:46:20Z")

</div>

You’re correct that your issue is because you’re ref-ing an ephemeral model. I would open an issue on the core repo for this - I suspect this is a bug as opposed to an intentional decision to not support accessing ephemeral models. If it is intentional then it should have a better error message at least!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex020/uploads/getdbt/original/1X/a7a7ca1fe379aaf90952b0e13118a817babcd14f.png) [@system](https://discourse.getdbt.com/u/system)
#### Post date: [April 4, 2023, 5:47am UTC](https://discourse.getdbt.com/t/python-model-injecting-sql/7490/4 "2023-04-04T05:47:19Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
