Headers already sent error with get_template_part in REST API call

Crosspost from StackOverflow

I’ve looked at multiple other questions about this warning (Headers already sent…etc) and this thorough explanation, but I’m not finding a solution for the circumstances I’m dealing with:

  1. I have a custom WordPress REST API endpoint that returns the output of get_template_part. When I just call get_template_part normally on a page, there’s no warning. It only appears when it runs in register_rest_route‘s callback.
  2. I am also fetching data from an external API using this library. The error started when I started making these requests. I’ve tried making the request both inside and outside the template part, the latter either within register_rest_route‘s callback or as an init action.
  3. The line that triggers the error is an img tag, where I’m echo-ing a URL as the src using data from the external API response. There are other echo calls all over this template, so I doubt that’s the issue.
  4. The line in question actually works fine and does its job. I just need to get rid of the accursed warning.

Code:

Inside functions.php:

<?php    //the api endpoint declaration   add_action( 'rest_api_init', function () {     register_rest_route(         'theme/v2',   // namespace         '/homepage',  // route         array(                  // options             'methods'  => 'GET',             'callback' => 'build_home',         )     );   });    //the callback for the wordpress api   function build_home() {      // this is the external API request, using their PHP library.      // I linked the library above. The request returns an object with a bunch of data     include_once(get_template_directory()."/arena/arena.php");     $  arena = new Arena();     $  page = $  arena->set_page();      $  per = 8;      $  slug = 'concepts-bo-6ajlvpqg';      $  channel = $  arena->get_channel($  slug, array('page' => $  page, 'per' => $  per));      //I'm passing the response from the external API (in $  channel) to get_template part     //get_template_part is then the output for the wordpress API request     get_template_part('custom-home-template',null,array('channel'=>$  channel));  }   ?> 

Inside template part:

<?php   $  channel=$  args["channel"];   ?>    <div id="concepts-box" class="bodycontent reg-width">     <?php foreach ($  channel->contents as $  item) { if($  item->class=="Image"){       $  img=$  item->image["square"]["url"];       ?>     <div class="concept-wrapper">       <!-- the line below is the one that triggers the error -->       <img lozad src="<?=$  img; ?>">     </div>     <?php }} ?>   </div> 

Full warning:

<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /filepath/wp-content/themes/theme/custom-home-template.php:63) in <b>/filepath/wp-includes/rest-api/class-wp-rest-server.php</b> on line <b>1372</b><br />