@@ -136,6 +136,56 @@ def _get_credential_from_config_file(key_name: str) -> Optional[str]:
136136 return None
137137
138138
139+ def _get_credentials_from_config_file () -> Dict [str , Optional [str ]]:
140+ """
141+ Retrieve both api_key and account_id from auth.ini with a single read/parse.
142+ Tries simple parsing first for both keys, then falls back to configparser for any missing ones.
143+ Returns a dict with up to two keys: 'api_key' and 'account_id'.
144+ """
145+ results : Dict [str , Optional [str ]] = {}
146+ auth_ini_path = _get_auth_ini_file ()
147+ if not auth_ini_path .exists ():
148+ return results
149+
150+ # 1) Simple key=value parsing
151+ try :
152+ simple_creds = _parse_simple_auth_file (auth_ini_path )
153+ if "api_key" in simple_creds and simple_creds ["api_key" ]:
154+ results ["api_key" ] = simple_creds ["api_key" ]
155+ if "account_id" in simple_creds and simple_creds ["account_id" ]:
156+ results ["account_id" ] = simple_creds ["account_id" ]
157+ if "api_key" in results and "account_id" in results :
158+ return results
159+ except Exception as e :
160+ logger .warning ("Error during simple parsing of %s: %s" , str (auth_ini_path ), e )
161+
162+ # 2) ConfigParser for any missing keys
163+ try :
164+ config = configparser .ConfigParser ()
165+ config .read (auth_ini_path )
166+ for key_name in ("api_key" , "account_id" ):
167+ if key_name in results and results [key_name ]:
168+ continue
169+ if "fireworks" in config and config .has_option ("fireworks" , key_name ):
170+ value_from_file = config .get ("fireworks" , key_name )
171+ if value_from_file :
172+ results [key_name ] = value_from_file
173+ continue
174+ if config .has_option (config .default_section , key_name ):
175+ value_from_default = config .get (config .default_section , key_name )
176+ if value_from_default :
177+ results [key_name ] = value_from_default
178+ except configparser .MissingSectionHeaderError :
179+ # Purely key=value file without section headers; simple parsing should have handled it already.
180+ logger .debug ("%s has no section headers; falling back to simple parsing results." , str (auth_ini_path ))
181+ except configparser .Error as e_config :
182+ logger .warning ("Configparser error reading %s: %s" , str (auth_ini_path ), e_config )
183+ except Exception as e_general :
184+ logger .warning ("Unexpected error reading %s: %s" , str (auth_ini_path ), e_general )
185+
186+ return results
187+
188+
139189def get_fireworks_api_key () -> Optional [str ]:
140190 """
141191 Retrieves the Fireworks API key.
@@ -184,7 +234,8 @@ def get_fireworks_account_id() -> Optional[str]:
184234 """
185235 # If a profile is active, prefer profile file first, then env
186236 if _is_profile_active ():
187- account_id_from_file = _get_credential_from_config_file ("account_id" )
237+ creds = _get_credentials_from_config_file ()
238+ account_id_from_file = creds .get ("account_id" )
188239 if account_id_from_file :
189240 return account_id_from_file
190241 account_id = os .environ .get ("FIREWORKS_ACCOUNT_ID" )
@@ -197,12 +248,14 @@ def get_fireworks_account_id() -> Optional[str]:
197248 if account_id :
198249 logger .debug ("Using FIREWORKS_ACCOUNT_ID from environment variable." )
199250 return account_id
200- account_id_from_file = _get_credential_from_config_file ("account_id" )
251+ creds = _get_credentials_from_config_file ()
252+ account_id_from_file = creds .get ("account_id" )
201253 if account_id_from_file :
202254 return account_id_from_file
203255
204256 # 3) Fallback: if API key is present, attempt to resolve via verifyApiKey (env or auth.ini)
205257 try :
258+ # Intentionally use get_fireworks_api_key to centralize precedence (env vs file)
206259 api_key_for_verify = get_fireworks_api_key ()
207260 if api_key_for_verify :
208261 resolved = verify_api_key_and_get_account_id (api_key = api_key_for_verify , api_base = get_fireworks_api_base ())
0 commit comments