code: def build_sagemaker_args(
      launch_project: LaunchProject,
      api: Api,
      role_arn: str,
      entry_point: Optional[EntryPoint],
      args: Optional[List[str]],
      max_env_length: int,
      image_uri: Optional[str] = None,
      default_output_path: Optional[str] = None,
  ) -> Dict[str, Any]:
      sagemaker_args: Dict[str, Any] = {}
      given_sagemaker_args: Optional[
          Dict[str, Any]
      ] = launch_project.resource_args.get("sagemaker")
  
      if given_sagemaker_args is None:
          raise LaunchError(
              "No sagemaker args specified. Specify sagemaker args in resource_args"
          )
      if (
          given_sagemaker_args.get("OutputDataConfig") is None
          and default_output_path is not None
      ):
          sagemaker_args["OutputDataConfig"] = {
              "S3OutputPath": default_output_path
          }
      else:
          sagemaker_args["OutputDataConfig"] = given_sagemaker_args.get(
              "OutputDataConfig"
          )
  
      if sagemaker_args.get("OutputDataConfig") is None:
          raise LaunchError(
              "Sagemaker launcher requires an OutputDataConfig Sagemaker resource argument"
          )
      training_job_name = cast(
          str,
          (
              given_sagemaker_args.get("TrainingJobName")
              or launch_project.run_id
          ),
      )
      sagemaker_args["TrainingJobName"] = training_job_name
      entry_cmd = entry_point.command if entry_point else []
  
      sagemaker_args[
          "AlgorithmSpecification"
      ] = merge_image_uri_with_algorithm_specification(
          given_sagemaker_args.get(
              "AlgorithmSpecification",
              given_sagemaker_args.get("algorithm_specification"),
          ),
          image_uri,
          entry_cmd,
          args,
      )
  
      sagemaker_args["RoleArn"] = role_arn
  
      camel_case_args = {
          to_camel_case(key): item
          for key, item in given_sagemaker_args.items()
      }
      sagemaker_args = {
          **camel_case_args,
          **sagemaker_args,
      }
  
      if sagemaker_args.get("ResourceConfig") is None:
          raise LaunchError(
              "Sagemaker launcher requires a ResourceConfig Sagemaker resource argument"
          )
  
      if sagemaker_args.get("StoppingCondition") is None:
          raise LaunchError(
              "Sagemaker launcher requires a StoppingCondition Sagemaker resource argument"
          )
  
      given_env = given_sagemaker_args.get(
          "Environment", sagemaker_args.get("environment", {})
      )
      calced_env = get_env_vars_dict(
          launch_project, api, max_env_length
      )
      total_env = {**calced_env, **given_env}
      sagemaker_args["Environment"] = total_env
  
      # Add wandb tag
      tags = sagemaker_args.get("Tags", [])
      tags.append({"Key": "WandbRunId", "Value": launch_project.run_id})
      sagemaker_args["Tags"] = tags
  
      # remove args that were passed in for launch but not passed to sagemaker
      sagemaker_args.pop("EcrRepoName", None)
      sagemaker_args.pop("region", None)
      sagemaker_args.pop("profile", None)
  
      # clear the args that are None so they are not passed
      filtered_args = {
          k: v for k, v in sagemaker_args.items() if v is not None
      }
  
      return filtered_args
  
imports: import asyncio
  import logging
  from typing import Any, Dict, List, Optional, cast
  import wandb
  from wandb.apis.internal import Api
  from wandb.sdk.launch.environment.aws_environment import AwsEnvironment
  from wandb.sdk.launch.errors import LaunchError
  from .._project_spec import EntryPoint, LaunchProject, get_entry_point_command
  from ..builder.build import get_env_vars_dict
  from ..registry.abstract import AbstractRegistry
  from ..utils import LOG_PREFIX, MAX_ENV_LENGTHS, PROJECT_SYNCHRONOUS, event_loop_thread_exec, to_camel_case
  from .abstract import AbstractRun, AbstractRunner, Status
label_desc: Use image node_id 2 for calling a global function with example usage: build_sagemaker_args(launch_project, api, role_arn, entry_point, args, max_env_length, image_uri, default_output_path) and returns: filtered_args