microsoftgraph / microsoftgraph/msgraph-sdk-python

Bug: AttributeError: 'function' object has no attribute 'headers' when using request_configuration lambda/function for messages.get() in v1.26.0

オープン
#1,175 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Needs: Attention :wave: priority:p3
主要言語
Python
スター
630
フォーク
96
平均マージ
15時間 20分
マージ済み PR(30日)
3

説明

Describe the bug

When attempting to use the request_configuration parameter with the .get() method on a MessagesRequestBuilder (specifically graph_client.users.by_user_id(...).mail_folders.by_mail_folder_id(...).messages.get()), providing a lambda function or a named function to configure query parameters (like $select, $top, $skip, $orderby) results in an AttributeError: 'function' object has no attribute 'headers'.

This error occurs internally within the Kiota request adapter's configure method (kiota_abstractions\request_information.py), indicating that the SDK is incorrectly trying to access attributes on the configuration function itself, rather than executing the function to modify the internal request configuration object before attempting to access its attributes.

Calling the same .get() method without the request_configuration parameter works successfully (confirming authentication and base Mail.Read permissions are correct), but this prevents necessary customization like pagination and field selection.

Attempts to import and use explicit configuration classes like MessagesRequestBuilderGetQueryParameters also fail with an ImportError in this version, suggesting the lambda/function approach is the intended method, but it appears to be broken for this endpoint/version combination.

Expected behavior

The messages.get() call should execute the provided lambda function (request_config_lambda) against an internal request configuration object. The query parameters specified within the lambda ($select, $top, $orderby) should be applied to the outgoing Graph API request. The request should succeed (assuming correct permissions), returning the specified number of messages with the selected fields, ordered accordingly.

Actual Behavior:

The script fails with an AttributeError inside the Kiota request adapter before the API call is made:

AttributeError: 'function' object has no attribute 'headers'
Traceback (most recent call last):
File "...\minimal_repro.py", line 41, in main # Adjust line number based on final script
messages_response = await graph_client.users.by_user_id(user_id).mail_folders.by_mail_folder_id("inbox").messages.get(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
request_configuration=request_config_lambda # <--- FAILS HERE
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
)
^
File "....venv\Lib\site-packages\msgraph\generated\users\item\mail_folders\item\messages\messages_request_builder.py", line 57, in get
request_info = self.to_get_request_information(
request_configuration
)
File "....venv\Lib\site-packages\msgraph\generated\users\item\mail_folders\item\messages\messages_request_builder.py", line 101, in to_get_request_information
request_info.configure(request_configuration)
~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
File "....venv\Lib\site-packages\kiota_abstractions\request_information.py", line 93, in configure
self.headers.add_all(request_configuration.headers)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'function' object has no attribute 'headers'

How to reproduce

Set up a virtual environment with msgraph-sdk==1.26.0 and its dependencies (azure-identity, python-dotenv, microsoft-kiota-abstractions, etc.).

Create a .env file with valid AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, and MAILBOX_USER_ID (for a mailbox the App Registration has Mail.Read Application permission + Admin Consent for).

Run the following minimal Python script:

import os
import asyncio
import logging
from dotenv import load_dotenv
from azure.identity import ClientSecretCredential
from msgraph import GraphServiceClient
from kiota_abstractions.api_error import APIError # Or ODataError if preferred

Basic Logging

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logging.getLogger("azure.identity._internal.get_token_mixin").setLevel(logging.ERROR)

Load .env

load_dotenv()
tenant_id = os.getenv('AZURE_TENANT_ID')
client_id = os.getenv('AZURE_CLIENT_ID')
client_secret = os.getenv('AZURE_CLIENT_SECRET')
user_id = os.getenv('MAILBOX_USER_ID')

if not all([tenant_id, client_id, client_secret, user_id]):
logging.error("Missing environment variables")
exit()

async def main():
logging.info("Initializing client...")
try:
credential = ClientSecretCredential(tenant_id, client_id, client_secret)
scopes = ['https://graph.microsoft.com/.default']
graph_client = GraphServiceClient(credentials=credential, scopes=scopes)
logging.info("Client initialized. Fetching messages...")

    # --- This configuration causes the AttributeError ---
    request_config_lambda = lambda request_config: (
        hasattr(request_config, 'query_parameters') and request_config.query_parameters is not None and (
            setattr(request_config.query_parameters, 'select', ["id", "subject", "receivedDateTime"]),
            setattr(request_config.query_parameters, 'top', 5),
            setattr(request_config.query_parameters, 'orderby', ["receivedDateTime desc"])
        )
    )
    # --- End Problematic Configuration ---

    messages_response = await graph_client.users.by_user_id(user_id).mail_folders.by_mail_folder_id("inbox").messages.get(
        request_configuration=request_config_lambda # <--- FAILS HERE
    )

    # This part is never reached due to the error
    if messages_response and messages_response.value:
        logging.info(f"Successfully fetched {len(messages_response.value)} emails.")
        for msg in messages_response.value:
            logging.info(f" - {msg.received_date_time} : {msg.subject}")
    else:
        logging.info("No messages found or response was empty.")

except APIError as api_error:
    logging.error(f"API Error: Status={getattr(api_error, 'response_status_code', 'N/A')}, Message={api_error}", exc_info=True)
except Exception as e:
    logging.error(f"Unexpected Error: {e}", exc_info=True)

if name == "main":
asyncio.run(main())

SDK Version

1.26

Latest version known to work for scenario above?

None

Known Workarounds

Removing the request_configuration parameter entirely allows the messages.get() call to succeed, fetching the default fields and default page size. However it defeats the purpose of the app in order to collect all the data.

This works, but lacks customization:

messages_response = await graph_client.users.by_user_id(user_id).mail_folders.by_mail_folder_id("inbox").messages.get()

Debug output
Click to expand log ```
</details>


### Configuration

msgraph-sdk version: 1.26.0

microsoft-kiota-abstractions version: 1.9.3

azure-identity version: 1.21

Python version: 3.13.2

Operating System: Windows 11

### Other information

_No response_

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

生成された users/item/mail_folders/item/messages/messages_request_builder.py エントリポイントと kiota_abstractions/request_information.py の configure メソッドから始め、提供された最小限の Python スクリプトと msgraph-sdk 1.26.0 の依存関係を使って失敗を再現します。完了条件は、request_configuration が AttributeError なしで処理され、select、top、orderby の値が送信される messages リクエストに到達することです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
api
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。